c# - EF code-first 1:* 1:0..1 relationship between the same models -
i have 2 models
public class a{ public aid { get; set; } public int? mainbid { get; set; } [foreignkey("mainbid")] public b mainb { get; set; } } public class b{ public int bid { get; set; } public int? aid { get; set; } [foreignkey("aid")] public virtual owner { get; set;} }
the relationship want model has many b. may have specially designated b (main b).
however way have set has hard time identifying principal , using fluent:
modelbuilder .entity<a>() .hasoptional(x => x.mainb) .withoptionaldependent();
it gives me multiplicity not valid in role 'a_mainb_source' in relationship 'a_mainb'. because dependent role properties not key properties, upper bound of multiplicity of dependent role must '*'.
i think trouble trying describe ef these 2 separate relationships , not same relationship described front-and-back. can guide me in this?
edit: adds real life example
i have need model series of teaching courses (each called "module"). "modules" represent webpage conveying information particular course. each module can have many "resource"s (downloadable binary files).
some modules can have resource represents same information webpage. in modules can have resource can printed out , consumed instead of sitting in front of machine reading through webpage. pdf version of webpage.
so in above each module has many resources. each module can optionally have "special" resource needs called out separately wherever other resources used.
i considered having flag on resource indicate resource special in way allow 0..* not want. thought might cleaner. have far not been able work ef.
this working example:
public class module { public int id { get; set; } public string name { get; set; } public icollection<resource> resources { get; set; } public int? specialresourceid { get; set; } public resource specialresource { get; set; } } public class resource { public int id { get; set; } public int? moduleid { get; set; } public module module { get; set; } public virtual module isspecialformodule { get; set; } } public class mycontext : dbcontext { public virtual dbset<module> modules { get; set; } public virtual dbset<resource> resources { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<module>() .hasmany(x=>x.resources) .withoptional(x=>x.module) .hasforeignkey(x=>x.moduleid) .willcascadeondelete(false); modelbuilder.entity<resource>() .hasoptional(x=>x.isspecialformodule) .withoptionaldependent(x=>x.specialresource) .willcascadeondelete(false); } }
if special resource, can special many modules model changing this:
public class resource { public int id { get; set; } public int? moduleid { get; set; } public module module { get; set; } public virtual icollection<module> isspecialformodules { get; set; } }
and config. :
modelbuilder.entity<resources>() .hasmany(x=>x.isspecialformodules ) .withoptional(x=>x.specialresource ) .hasforeignkey(x=>x.specialresourceid ) .willcascadeondelete(false);
Comments
Post a Comment