c# - Difficulty Concerning EF Code First Fluent API, TPH, and Foreign Keys -
c# - Difficulty Concerning EF Code First Fluent API, TPH, and Foreign Keys -
i have 2 tables in database. 1 called users
, , other called widgets
. widgets
table represents 3 entities in code model. 1 of entities, widget
, parent class other 2 entities, widgettypea
, widgettypeb
. both widgettypea
, widgettypeb
have navigation properties user entity, persisted users table in database. i'm having problem getting code first utilize same foreign key both widgettypea
, widgettypeb
entities (userid
). know how this? seems should mutual problem table per hierarchy mapping.
my entity classes follows:
public class widget { public int id { get; set; } public string name { get; set; } } class widgetmap : entitytypeconfiguration<widget> { public widgetmap() { totable("widgets"); haskey(w => w.id); property(w => w.id) .hasdatabasegeneratedoption(databasegeneratedoption.identity); property(w => w.name) .isrequired() .hasmaxlength(75) .isunicode(true); } } public class widgettypea : widget { public int userid { get; set; } public virtual user user { get; set; } public string color { get; set; } public int depthlevel { get; set; } } class widgettypeamap : entitytypeconfiguration<widgettypea> { public widgettypeamap() { map(w => w.requires("widgettypeid").hasvalue(1)); hasrequired(w => w.user) .withmany(u => u.widgettypeas) .hasforeignkey(w => w.userid) .willcascadeondelete(false); property(w => w.color) .isoptional() .isunicode(true) .hasmaxlength(75); property(w => w.depthlevel) .isoptional(); } } public class widgettypeb : widget { public int userid { get; set; } public virtual user user { get; set; } } class widgettypebmap : entitytypeconfiguration<widgettypeb> { public widgettypebmap() { map(w => w.requires("widgettypeid").hasvalue(2)); hasrequired(w => w.user) .withmany(u => u.widgettypebs) .hasforeignkey(w => w.userid) .willcascadeondelete(false); } } public class user { public int id { get; set; } public string username { get; set; } public int age { get; set; } public virtual icollection<widgettypea> widgettypeas { get; set; } public virtual icollection<widgettypeb> widgettypebs { get; set; } } class usermap : entitytypeconfiguration<user> { public usermap() { totable("users"); haskey(u => u.id); property(u => u.username) .isrequired() .hasmaxlength(75) .isunicode(true); property(u => u.age) .isrequired(); } }
at rate, maintain getting error
invalid column name 'userid1'
when seek perform next operations:
using (var entities = new myentities()) { user u = new user { username = "frank", age = 14 }; entities.users.add(u); entities.savechanges(); widgettypea wa1 = new widgettypea { name = "0sdf81", userid = u.id, depthlevel = 6 }; entities.widgettypeas.add(wa1); entities.savechanges(); }
not sure if can fixed or not. can specify sec userid foreign key widgets table, seems pointless. perhaps there's way using fluent api?
you cannot map properties defined in different derived entities same column. limitation in ef. if widgettypea
has userid
property , widgettypeb
has userid
property must different columns in database. should work if move both userid
, user
properties derived types parent widget
type.
c# entity-framework entity-framework-4.1 ef-code-first
Comments
Post a Comment