【发布时间】:2016-01-09 16:45:25
【问题描述】:
我目前正在学习 ASP.NET MVC 和 Web API。
我正在尝试创建用户模型。用户可以拥有任意数量的 UserContact。 UserContacts 引用作为联系人的用户和作为联系人的用户。我制作了一个名为 UserContact 的模型,因为附加到此模型的是附加信息。
public class User
{
public int UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class UserContact
{
public int UserContactID { get; set; }
public int UserID { get; set; }
[ForeignKey("UserID"), Column(Order = 0)]
[Required]
public User User { get; set; }
public int ContactID { get; set; }
[ForeignKey("ContactID"), Column(Order = 1)]
[Required]
public User Contact { get; set; }
public DateTime ContactSince { get; set; }
}
所以这给了我一个关于级联删除的错误。如何建立这样的关系,其中两个外键指向相同的模型类型?我还没有掌握实体框架的语法。如果我在 User 模型中没有 ICollection UserContacts,这是否会妨碍我获取与该用户关联的 UserContacts 的能力?
【问题讨论】:
标签: asp.net-web-api ef-code-first