c# - Entity framework map column which is not in old database -
i have table in new db contains column 'note'. have old db same table same structure have not column 'note'. changed edmx, added (mapped) column 'note' new db. if want use edmx in old db have error: column doest not exists...
i try put code try catch, without succesfull. error outside off try catch.
//---new version db try { vehicles = entities.vehicle.where(v => v.numplate == numplate || v.note == numplate); } catch (exception) { vehicles = entities.vehicle.where(v => v.numplate == numplate); } //---old version db foreach (vehicle vehicle in vehicles) //<------- error how can resolve? thanks
that's kind of 'hack', can choose ignore note property once you're using old database.
look onmodelcreating method in context class, , replace with:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { if (this.database.connection.database == "name_of_old_database") modelbuilder.entity<vehicle>().ignore(x => x.note); } update:
since you're using .edmx mapping , there's no way modify mapping in runtime assume suggestion indeed wrong.
i still don't trying achieve using try/catch block, doesn't matter whether include (missing) note field in query or not, since entity framework realizes model no longer compatible database, refuse work database altogether.
but 2 points keep in mind:
since you're using
iqueryable,try/catchindeed in wrong place, exception thrown duringmaterialization(when trying retrieving data).you can materialize query using
tolistmethod (among others):entities.vehicle.where(v => v.numplate == numplate || v.note == numplate).tolist();or move
try/catchblock wrapforeachstatement.you can use
system.data.entity.database.compatiblewithmodel()method determine (upon context initialization example) whether model , database can still work together.
Comments
Post a Comment