c# - EF 6 table mapping error -
i'm running error trying save data in mvc app. we're using code first.
i'm saving data this:
var fielddefinition = db.customfields .firstordefault(x => x.customfieldid == thisresp.customfieldid); var newdata = new customdata { projectid = new guid("280288d7-7630-e511-8420-00215e466552"), customfieldid = thisresp.customfieldid }; if (fielddefinition.allowmultiple) { var values = thisresp.value.split(','); foreach (var thisvalue in values) { var newmulti = new customdatamulti { customdataid = newdata.customdataid, customvalue = thisvalue }; db.customdatamulti.add(newmulti); } } db.customdata.add(newdata); db.savechanges();
however, message:
unable determine principal end of 'publicationsystem.model.customdata_customdatamultis' relationship. multiple added entities may have same primary key.
my classes set this:
public partial class customdata : baseentity { [key] public int customdataid { get; set; } public guid projectid { get; set; } public guid customfieldid { get; set; } //... public virtual icollection<customdatatext> customdatatexts { get; set; } public virtual icollection<customdatamulti> customdatamultis { get; set; } }
customdatamapping.cs
public customdatamapping() { //primary key haskey(t => t.customdataid); //constraints property(e => e.customvalue).isunicode(false); hasmany(e => e.customdatatexts) .withrequired(e => e.customdata) .willcascadeondelete(false); hasmany(e => e.customdatamultis) .withrequired(e => e.customdata) .willcascadeondelete(false); totable("customdata"); }
customdatamulti.cs
[table("customdatamulti")] public partial class customdatamulti : baseentity { [key] public int customdatamultiid { get; set; } public int customdataid { get; set; } [required] [stringlength(150)] public string customvalue { get; set; } public virtual customdata customdata { get; set; } }
customdatamultimapping.cs
public customdatamultimapping() { //primary key haskey(t => t.customdatamultiid); //constraints property(e => e.customvalue).isunicode(false); totable("customdatamulti"); }
i'm not sure i'm doing wrong.
entityframework doesnt understand principal end of relationship between following entities :-
customdata , customdatamulti.
this because relationship between 2 entities must have 1 side of relationship that's constant.
in case, customdata entity can have multiple customdatamulti objects. (list).
but can customdatamulti ever exist without belonging customdata object?
what need change customdatamulti class virtual property customdata required.
see below :-
[table("customdatamulti")] public partial class customdatamulti : baseentity { [key] public int customdatamultiid { get; set; } public int customdataid { get; set; } [required] [stringlength(150)] public string customvalue { get; set; } [required] public virtual customdata customdata { get; set; } }
this convention entityframework uses determine relationships.
to give clearer example.
think of orderitem, orderitem belong order.
an orderitem without relevant order useless.
the order entity prinicpal end of relationship.
hope helps.
Comments
Post a Comment