c# - Multiplicity is not valid in Role in relationship: EF code first one to one relationship with same primary key and foreign key -
i have 2 entities 1 one relationship in target entity primary key , foreign key same. here 2 entity , fluent mapping.
public class register { public int id { get; set; } public string number { get; set; } // n.b: here can't have virtual property project dependencies. //public virtual customerdisplay customerdisplay { get; set; } } public class customerdisplay { public int registerid { get; set; } public double receiptviewpercent { get; set; } public virtual register register { get; set; } } public class registerconfiguration : entityconfig<register> { public registerconfiguration(bool useidentity, bool sqlserverce) : base(sqlserverce) { this.haskey(t => t.id); if (!useidentity) { property(d => d.id).isrequired().hasdatabasegeneratedoption(databasegeneratedoption.none); } this.property(t => t.number).isrequired().hasmaxlength(20); } } public class customerdisplayconfiguration: entityconfig<customerdisplay> { public customerdisplayconfiguration(bool sqlserverce) : base(sqlserverce) { this.haskey(t => t.registerid); this.hasrequired(t => t.register).withmany().hasforeignkey(d => d.registerid).willcascadeondelete(false); } }
i getting following error:
i have seen lots of related questions in stackoverflow didn't find solution. 1 best match issue:
how declare 1 one relationship ...
can tell me how can rid of issue. thanks
add again customerdisplay
navigation property:
public class register { public int id { get; set; } public string number { get; set; } public virtual customerdisplay customerdisplay { get; set; } }
and configure relationship show follow:
this.hasrequired(t => t.register).withoptional(r=>r.customerdisplay);
notice didn’t need use hasforeignkey
specify customerdisplay.customerid
fk. because of entity framework’s requirement primary key of dependent used foreign key. since there no choice, code first infer you.
update
if can't add customerdisplay
navigation property register
class, suggest create unidirectional 1 one relationship. use configuration:
this.hasrequired(t => t.register);
that enough tell ef principal , dependent entity in relationship.
Comments
Post a Comment