c# - Fluent API EF inheritance and mapping -


i've system several self referencing entities one-to-many relationship (parent-child). i'd use common base class of entities:

public class selfreferencing {   public selfreferencing parent {get; set;}   public icollection<selfreferencing> children {get; set;} } 

and inherit particular entity selfreferencing. fluent api mapping requires reference properties of defining type, when trying following:

modelbuilder.entity<concreteselfreferencing>()                 .hasmany(e => e.children)                 .withoptional(e => e.parent); 

so, can me find possibility make use of inheritance , entities mapped?

thx

note: example below known table-per-hierarchy (tph) - i.e. contained in 1 table. click on this link table-per-type (tpt), has different tables each type.

when using base types , inherited types, have tell ef how determine association specific inherited type.

taking code:

public abstract class selfreferencing {     public selfreferencing parent { get; set; }     public icollection<selfreferencing> children { get; set; } }  public class concreteselfreferencing : selfreferencing { } 

ef has work out whether sub-class concreteselfreferencing or other type of sub-class. determined discriminator on table itself, column is not part of mapping.

to take example, similar i've used in past:

public abstract class policy {    public int id { get; set; }    public string policynumber { get; set; } }  public class insurancepolicy : policy { }  public class carpolicy : policy { } 

the table structured this:

| id    |   policynumber  | type  |  ..... |   1         car0001         c   2         isn0001         

to ef result them correctly, have:

public class mycontext : dbcontext {    public mycontext() : base()    {    }     public dbset<policy> policies { get; set; }     protected override void onmodelcreating(modelbuilder builder)    {       var policymap = modelbuilder.entity<policy>();        // set discriminators       policymap.map<insurancepolicy>(p => o.requires("type").hasvalue("i"))                .map<carpolicy>(p => o.requires("type").hasvalue("c"));        // notice `type` used discriminators, not actual       // mapped property       policymap.haskey(x=>x.id);       policymap.property(x=>x.policynumber);    } } 

and code, can either filtering yourself, or put filtering in dbcontext. here example separate class.

public class policyrepository {    private mycontext context = new mycontext();     public policyrepository()    {    }     public iqueryable<insurancepolicy> getinsurancepolicies()    {       return this.context.policies.oftype<insurancepolicy>();    }     public iqueryable<carpolicy> getcarpolicies()    {       return this.context.policies.oftype<carpolicy>();    } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -