我有两个实体类,Contact和User.
User具有ContactID作为外键的Contact. 我可以显示Userto的关系Contact。
现在我还需要Contact与表User中的相同外键建立关系User(而不是Contact)
public class Contact{
public int ContactID {get;set;}
// This relation doesn't work
// Need to Make this Work
public User ContactUser {get;set;}
}
public class User {
public string Id {get;set;}
public int? ContactID {get;set;}
// This Relation works
[ForeignKey("ContactID")]
public Contact Contact {get;set;}
}
所以我需要让这种ContactUser关系继续下去Contact。我应该使用什么样的映射?
编辑
正如我所建议的那样:
modelBuilder.Entity<Contact>().HasRequired(c => c.ContactUser).WithOptional(pi => pi.Contact);
我从中删除了ForeignKey属性User,它导致Multiplicity...error
但我得到错误喜欢: Invalid column name 'User_Id'.