我知道也有类似的问题包含此错误消息,但是情况与其他情况略有不同。
我需要在客户之间建立关系。但这与建立父子关系无关,而不是具有描述性关系。
示例:
客户A是客户B和C的购买者 关系是“购买者”,A是关系的所有者,B和C是成员
客户C是客户A和B的交付经理 关系是“ Deliverymanager”,C是关系的所有者,A和B是成员
因此在数据库中,它应该类似于:
表关系 ID(PK)|标题|所有者(从FK到客户)
表Relationship_Member RelationshipId(FK)| MemberId(从FK到客户)
表客户 Id(Pk)| RelationshipOwner(FK)| RelationshipMember(FK)
我的模型如下:
public class Relationship
{
public int Id { get; set; }
public string Title { get; set; }
public virtual Customer Owner { get; set; }
public virtual ICollection<Customer> Members { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Relationship> RelationshipOwnerships { get; set; }
public virtual ICollection<Relationship> RelationshipMemberships { get; set; }
}
上下文:
modelBuilder.Entity<Relationship>()
.HasRequired<Customer>(rs => rs.Owner)
.WithMany(c => c.RelationshipOwnerships)
.HasForeignKey(rs => rs.OwnerId);
modelBuilder.Entity<Relationship>()
.HasMany<Customer>(rs => rs.Members)
.WithMany(c => c.RelationshipMemberships)
.Map(rsc =>
{
rsc.MapLeftKey("RelationshipId");
rsc.MapRightKey("CustomerId");
rsc.ToTable("RelationshipMembershipCustomers");
});
modelBuilder.Entity<Relationship>()
.HasRequired(rs => rs.Members)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Customer>()
.HasOptional(c => c.RelationshipMemberships)
.WithMany()
.WillCascadeOnDelete(false);
那么,有人知道吗?我想我需要更新流利的代码,但是我没有头绪。
查询时
Context.Relationships.Where(e => e.Owner.Id == 1)
.Include(e => e.Members)
.ToList();
我收到错误消息
声明的导航属性类型与指定的导航结果不兼容