c# - Entity Framework 1:0..1 relation in mvc 5 -
i wanted create relation between 2 of models 1:0..1 got 1..* relation:
public class medicalexamination { public int id { get; set; } public datetime entrydate { get; set; } public datetime executiondate { get; set; } public datetime acceptancedate { get; set; } public string description { get; set; } public string state { get; set; } public string result { get; set; } public string comment { get; set; } public virtual visit visit { get; set; } [required] public virtual examinationdictionary examinationdictionary { get; set; } }
and second model:
public class examinationdictionary { [key] public string code { get; set; } public string name { get; set; } public string description { get; set; } public decimal price { get; set; } public virtual medicalexamination medicalexamination { get; set; } }
and after ran , updated database got relation this: http://scr.hu/11m6/4eny0 thing 0..1:1 relation. know solution ?
a 1-to-1 or 1-to-0or1 relation in entity framework possible if both tables share same primary key.
so example, medicalexamination presumably principal entity in relationship. has id column primary key. examinationdictionary table needs have id column primary.
you describe relationship using fluent api so:
modelbuilder.entity<medicalexamination>() .hasoptional(m=>m.examinationdictionary) .withrequiredprincipal(d=>d.medicalexamination);
Comments
Post a Comment