c# - How does EF map non-abstract base types when ToTable isn't called? -


i using ef5 code-first entity classes so:

public class base {     public int id { get; set; } }  public class derived : base { // there other derived types } 

and configure derived entity follows:

var config = new entitytypeconfiguration<base>(); config.map<derived>(m => {     m.mapinheritedproperties();     m.totable("derived"); });  dbmodelbuilder modelbuilder = ... modelbuilder.configurations.add(config); 

in application call:

new mydbcontext().set<derived>().first(); 

what expected behavior call?

weirdly, seem getting inconsistent behavior hierarchies configured same way. fails because tries query "dbo.base" , correctly queries "dbo.derived".

the default value ef derive type mapping table per hierarchy. can defaults on blog

on base table new field called "discriminator" added add fields in derive type created in database nullable column.

in case, should tpt, table "base" contain fields of base class , table "derived" contains fields of derived class. both table have shared primary key.

from blog: there 3 different approaches representing inheritance hierarchy:

  • table per hierarchy (tph): enable polymorphism denormalizing sql schema, , utilize type discriminator column holds type information.
  • table per type (tpt): represent "is a" (inheritance) relationships "has a" (foreign key) relationships.
  • table per concrete class (tpc): discard polymorphism , inheritance relationships sql schema.

each of tree scenarios have pros , cons it's described in morteza manavi's blog


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -