entity framework - creating strict 1 to 1 relationship by code first -
i want create 1 1 relationship code first, below code,
class person { public int id { get; set; } public string name { get; set; } public virtual persondetail detail { get; set; } } class persondetail { public int id { get; set; } public double height { get; set; } public double weight { get; set; } public virtual person person { get; set; } } class eftest : dbcontext { public dbset<person> personset { get; set; } public dbset<persondetail> detailset { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<person>().hasrequired(x => x.detail).withrequiredprincipal(x => x.person); } } but still can insert person without person detail. i'm trying create 1 1 relationship in model first, works well, if insert 1 end without other end, there exception thrown. why code first code above create 1 0..1 relationship?
anyone can help?
that possible if both person , persondetail mapped same table (the mapping technique called table splitting) because strict 1:1 means cannot insert person without existing persondetail cannot insert persondetail without existing person => cannot insert either of them because dependency missing (remember each record has own insert command , database check integrity after each command not after transaction).
only when use table splitting ef create single insert command containing data both entities. in entity model 2 entities 1:1 mapping in database single table.
Comments
Post a Comment