c# - EF DB First refresh is not detecting FKs to a particular table -


not sure what's happened setup, of late, when update database new fields or new tables have foreign key particular table (person), database refresh picks new fields, not recognize fk relationships person. foreign keys other tables work fine.

what setting might missing?

perhaps clue: person acts base type several other tables, e.g. manager, customer etc. share basic characteristics of having name, birth date, gender etc., , descendant tables have primary key acts foreign key person. fk relationships descendant tables not being recognized.

entity framework can rather limited when mapping complex inheritance models. have 3 options when mapping inheritance:

tph (table per hierarchy) mapping: mapping generates single table fields in hierarchy , conflicting or "same-name" fields appended numberic seed. example name1 (person), name2 (manager) etc.

tpt (table-per-type) mappping: mapping generates individual tables each object, conflicting property names mapped in base class. in instance person class. note: tpt discouraged due complex join queries , in cases anomalies experiencing

tpc (table-per-concrete-type) mapping: similar tpt, except properties of class, including inherited properties, map columns of corresponding table

solution:

the assumption using tpt , means inherited class members not being mapped. suggest @ tpc generating mapping , migration. need @ fluent api in ef achieve above mentioned mappings:

tpc example:

declare in dbcontext object:

 protected override void onmodelcreating(modelbuilder modelbuilder)     {         modelbuilder.entity<person>()          .property(c => c.courseid)          .hasdatabasegeneratedoption(databasegeneratedoption.none);           modelbuilder.entity<manager>().map(m =>          {              m.mapinheritedproperties();              m.totable("manager");          });           modelbuilder.entity<customer>().map(m =>          {             m.mapinheritedproperties();             m.totable("customer");          });     } 

disclaimer: rather difficult know problem without seeing inheritance structure or executed migrations, find common issue in ef relationships mentioned above.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -