简体   繁体   English

实体框架中的自引用/父子关系

[英]Self referencing / parent-child relationship in Entity Framework

I read quite a number of posts of programmers that run into the Unable to determine a valid ordering for dependent operations. 我读了许多程序员的文章,这些文章遇到了无法确定相关操作的有效顺序的问题。 Dependencies may exist due to foreign key constraints, model requirements, or store-generated values -exception when using a self-referencing relationship in Entity Framework. 在Entity Framework中使用自引用关系时,可能由于外键约束,模型要求或存储生成的值而存在依赖关系。

I am trying to get a parent-child relationship to work: 我正在尝试建立亲子关系:

public class Category {
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public int ParentId { get; set; }
    public Category Parent { get; set; }
    public List<Category> Children { get; set; }
}

This is the configuration I use (Fluent API): 这是我使用的配置(Fluent API):

Property(c => c.ParentId).IsOptional();
HasMany(c => c.Children).WithOptional(c => c.Parent).HasForeignKey(c => c.ParentId);
//HasOptional(c => c.Parent).WithMany(c => c.Children).HasForeignKey(c => c.ParentId);

Both the HasMany() and HasOptional() configurations result in a "Unable to determine a valid ordering for dependent operations..." exception when I try to save a new category like this: 当我尝试保存这样的新类别时,HasMany()和HasOptional()配置都会导致“无法确定相关操作的有效顺序...”异常:

context.Categories.Add(new Category { Name = "test" });

I don't understand why EF doesn't insert the Category with a null parentId. 我不明白为什么EF不插入带有null parentId的Category。 The database allows the ParentId foreign key to be null. 数据库允许ParentId外键为空。

Would you be able to tell me how to do this? 您能告诉我该怎么做吗?

You must define the ParentId in the category class as nullable to use it as the foreign key property for an optional relationship: 您必须在类别类中将ParentId定义为可空值,以将其用作可选关系的外键属性:

public int? ParentId { get; set; }

An int property cannot take the value null and therefore cannot represent a NULL as value in a database column. int属性不能采用null ,因此不能将NULL表示为数据库列中的值。

暂无
暂无

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

相关问题 实体框架自参考-父子 - Entity Framework self referencing - Parent-Child 实体框架:如何在自引用父子关系上指定外键列的名称? - Entity Framework: How to specify the name of Foreign Key column on a self-referencing Parent-Child relationship? 如何使用实体框架保持父子C#关系 - How to hold Parent-Child C# relationship with Entity Framework 具有自引用数据结构的树形视图UI关系(分层[父级-子级]关系) - Tree View UI Relationship with Self Referencing Data Structure(Hierarchical[parent-child] Relationship) 如何在 Entity Framework Core 2.2 中使用预先加载策略实现自递归父子关系数据加载? - How to implement self-recursive parent-child relationship data loading with eager loading policy in Entity Framework Core 2.2? Entity Framework中带有子项的自引用关系包括 - Self-referencing relationship in Entity Framework with child includes 实体框架代码首次自我引用带有效负载的父子 - Entity Framework Code First Self Referencing Parent Child with Payload 使用实体框架在层次结构中映射父级子级 - Map Parent-Child in Hierarchy with Entity Framework 在Entity Framework CF 5中实现两个或多个模型之间的一对多父子关系 - Implement one-to-many, parent-child relationship between two or more models in Entity Framework CF 5 当不存在显式关系时,使用实体框架请求父子对象集合 - Use Entity Framework to request parent-child object collection when no explicit relationship exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM