【发布时间】:2017-07-18 17:17:51
【问题描述】:
我正在对联系信息结构进行建模,但还没有完全弄清楚应该如何使用 EF Core 对关系进行编码。我对使用 EF 进行数据访问层相当陌生。
我想要一个可以包含网站、电话号码、电子邮件或社交信息的联系人模型。然后联系信息将被添加到几个不同的模型中。任何建议都会有所帮助,我不确定如何使用多表关系对这个一对多进行编码,或者是否可以使用 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 表,因为它本身已经是一对多关系。让我知道我可以给你代码示例。
-
没错,那太棒了。
-
这将很容易使用 EF Core 2.0 中的自有实体类型进行映射
标签: asp.net-core entity-framework-core