简体   繁体   English

实体框架中的0对1关系

[英]0 to 1 relationship in Entity Framework

Using EF6 Database First, I'm trying to add an association for an entity. 首先使用EF6数据库,我尝试为实体添加关联。 The entity is: 实体是:

public class Customer
{
    public int Id {get; set;}
    public string Name {get; set;}
    public int? ParentId {get; set;}
    public virtual Customer Parent {get; set;}
}

The Customer entity can have 0..1 related customer as Parent. 客户实体可以有0..1个相关客户作为父级。 After I add the navigation property and the association in the designer, and it generates the classes again, I get the following error: 在设计器中添加导航属性和关联,并再次生成类后,出现以下错误:

Running transformation: Multiplicity is not valid in Role 'Customer1' in relationship 'FK_customers_customers'. 运行转换:多重性在关系'FK_customers_customers'中的角色'Customer1'中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *. 因为从属角色属性不是关键属性,所以从属角色多重性的上限必须为*。

I obviously don't want a 1..n relationship because then I have a collection of parents in my customer. 我显然不想要1..n关系,因为那时我的客户中有很多父母。

Is there a workaround? 有解决方法吗?

Have you tried creating a collection for the children? 您是否尝试过为孩子们制作一个收藏品? Like: 喜欢:

public virtual ICollection<Customer> Children { get; protected set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM