hopefully can show me how implement many-to-may relation...
i have 2 classes, car , owner. each car can owned many owners. , each owner can own many cars. note want navigationproperty car owners.
public partial class car { public int carid { get; set; } public string name { get; set; } public icollection<owner> owners { get; set; } } public partial class owner { public int ownerid { get; set; } public string name { get; set; } }
in dbinitializer seed b this.
owner owner1 = new owner() { ownerid = 10, name = "erik" }; context.owners.add(owner1); car b1 = new car() { carid = 1, name = "volvo", owners = new list<owner>(new owner[] { owner1 }) }; car b2 = new car() { carid = 2, name = "saab", owners = new list<owner>(new owner[] { owner1 }) }; context.cars.add(b1); context.cars.add(b2); context.savechanges();
the result owner1 owns b2. why? , how can changed many many relation?
thanks!
in context add this:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.entity<car>().hasmany(c => c.owners).withmany(); }
i tells ef there many-to-many association between car
, owner
without navigation property in owner
.
Comments
Post a Comment