我有实体联系人必须链接到自身而不是分层模式
public partial class Contact
{
[Key, Column(Order = 0)]
public int AgentId { get; set; }
[Key, Column(Order = 1)]
public int ContactId { get; set; }
public virtual Contact Opposite { get; set; }
public ..... many other properties
}
每个联系人都有相反的Contact
链接ON c1.AgentId = c2.ContactId AND c1.ContactId = c2.AgentId
。相反的接触是可选的,但当它存在时,它们是一对一的。不是亲子。此关系的名称必须是Opposite
。我已经在Contact
课堂上声明了财产。现在我正在尝试设置关系,但这不起作用,我觉得我不知道如何正确设置它。请指教?
public class EFDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Action<ForeignKeyAssociationMappingConfiguration> mapKey = x => x.MapKey("ContactId", "AgentId");
modelBuilder.Entity<Contact>().HasOptional<Contact>(c => c.Opposite).WithRequired().Map(mapKey);
}
}