Entity Framework Many-to-many relationships, with the same link entity -
i trying reuse out of class holds many-to-many relationship data between number of entities.
you can enough relationship pks of 2 entities using map
class a{ icollection<item> items } class b{ icollection<item> items } class item{ string text } then in config this
entity<a>().hasmany(e=>e.items).withmany().map(..); entity<b>().hasmany(e=>e.items).withmany().map(..); this produces new link table , b
now, store more info on link tables, common
class a{ icollection<linkitem> items } class b{ icollection<linkitem> items } class linkitem{ int extrainfo item text } class item{ string text } this fails linkitem created in single table , complains relationships
"entities in 'datacontext.linkitem' participate in 'a_items' relationship. 0 related 'a_items_source' found. 1 'a_items_keywords_source' expected."
you can specify table in map method, m.totable("aitems"), fails
"the specified table 'aitems' not found in model. ensure table name has been correctly specified."
am allowed reuse linkitem class somehow?
(it doesn't need accessed set datacontext)
i believe want can done, i've tried using ef5, following model.
public class userscontext : dbcontext { public dbset<a> { get; set; } public dbset<b> bs { get; set; } public dbset<linkitem> linkitems { get; set; } public dbset<item> items { get; set; } } public class a{ public virtual int id { get; set; } public virtual icollection<linkitem> items { get; set; } } public class b{ public virtual int id { get; set; } public virtual icollection<linkitem> items { get; set; } } public class linkitem{ public virtual int id { get; set; } public virtual datetime extrainfo { get; set; } public virtual item text { get; set; } } public class item{ public virtual int id { get; set; } public virtual string text { get; set; } } this resulted in following db model

i've not added custom mappings used inferred model. work you?
if want avoid proliferation of foreign keys on linkitems table, can make many-2-many relationship entities , b following dbmodelbuilder commands add implicit link tables.
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<a>() .hasmany(t => t.items) .withmany(); modelbuilder.entity<b>() .hasmany(t => t.items) .withmany(); }
Comments
Post a Comment