c# - How to avoid mapping all children of a TPT inheritance in Code First when needing just one of them in EF 4.4? -


i have table per type inheritance implemented in system. used main framework use in app, shouldn't modify structure, but, time time, new children may implemented.

up now, didn't need reference models directly our business services , classes, 1 of them. have "contentitem" table parent, around 15 tables children. need use 1 child (an consequence, parent).

as we're using ef code first, i've defined mappings required child.

        modelbuilder.entity<contentitemc>().totable("contentitemc");         modelbuilder.entity<contentitemc>().haskey(c => c.contentitemid);         modelbuilder.entity<contentitemc>().hasrequired(c => c.anothermodel).withmany().hasforeignkey(c => c.anothermodel); 

the thing is, once this, need map parent table, need use relationships other models references in parent (contentitemtag instance). that:

modelbuilder.entity<contentitem>().totable("contentitem");             modelbuilder.entity<contentitem>().haskey(q => q.contentitemid);             modelbuilder.entity<contentitem>().ignore(ci => ci.featurelevel);             modelbuilder.entity<contentitem>().hasrequired(q => q.status).withmany().hasforeignkey(q => q.statusid);             modelbuilder.entity<contentitem>().hasmany(q => q.tags).withmany()                 .map(m => m.mapleftkey("contentitemid").maprightkey("tagid").totable("contentitemtag")); 

but here's when "issue" comes up. every time try or add contentitemc from/to ef context, application breaks saying properties not found. interestingly, these properties defined in other children models. looks when adding parent model code first, i'm required map rest of models. once this:

  modelbuilder.entity<contentitema>().totable("contentitema");             modelbuilder.entity<contentitema>().haskey(c => c.contentitemid);    modelbuilder.entity<contentitemb>().totable("contentitemb");             modelbuilder.entity<contentitemb>().haskey(c => c.contentitemid);    modelbuilder.entity<contentitemd>().totable("contentitemd");             modelbuilder.entity<contentitemd>().haskey(c => c.contentitemid);  etc. 

it looks dirty forced add each of these references models didn't need declare before in code first , app won't use directly (but break when trying use "contentitemc"). apart that, problem everytime contentitem"x" added, we're forced add dummy declaration in onmodelcreating method. worse, generate runtime error when trying access contentitemc, easy forget update code first result, specially haven't worked on contentitemc business.

i've thought of generating these modelbuilder rules reflection, don't find safe , clean solution either (and quite cryptical think).

any suggestions? thanks!


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -