我正在建模联系人信息结构,并没有完全弄清楚如何使用EF Core编码关系。我对使用EF进行数据访问层相当新颖。Asp核心多个实体关系
我想要一个可以包含网站,电话号码,电子邮件或社交信息的联系人模式。然后联系信息将被添加到几个不同的模型。任何建议都会有所帮助,我不知道如何使用许多表关系来编码这个One to many,或者甚至可以使用EF。
模型到目前为止
public class Contact
{
public String Id { get; set; }
public Int32 ContactType { get; set; } //Enum for Website, Phonenumbers, Emails, or Social
public String RecId { get; set; } //FK to multiple Models
public String RecType { get; set; }//Value for which model the RecID is for
public String Name { get; set; }
public String Value { get; set; }
}
public class ContactInfo
{
public virtual IList<Contact> Website { get; set; }
public virtual IList<Contact> PhoneNumbers { get; set; }
public virtual IList<Contact> Emails { get; set; }
public virtual IList<Contact> Socials { get; set; }
}
//Example of models to use the contact model
public class Company
{
....
pubic ContactInfo ContactInfo { get; set;}
}
public class Client
{
....
pubic ContactInfo ContactInfo { get; set;}
}
我知道你想与“公司:联系人”和“客户:联系人”表创建一对多的关系。是对的吗?那么你需要删除ContactInfo表,因为它已经在一对多的关系中。让我知道我可以给你代码示例。 – DSR
这是正确的,那会很棒。 –
这将非常容易使用EF Core 2.0中的所有者实体类型进行映射。 – Smit