简体   繁体   English

EF中的1:1自我关系

[英]1:1 self relation in EF

I have a object of type ScheduleItem 我有一个ScheduleItem类型的对象

 public class ScheduleItem : EntityBase
    {
        [MaxLength(500)]
        [Required]
        public string TitleAr { get; set; }

        [MaxLength(300)]
        [Required]
        public string TitleEn { get; set; }
        public int? ParentScheduleItemId { get; set; }


        public int? PreviousDestinationId { get; set; }
        public int? NextDestinationId { get; set; }

        public ScheduleItem ParentScheduleItem { get; set; }

        [InverseProperty("ParentScheduleItem")]
        public ICollection<ScheduleItem> ChildrenScheduleItems { set; get; }

        public ScheduleItem PreviousDestination { get; set; }
        public ScheduleItem NextDestination { get; set; }

}

This object is the parent object, inside it there's a collection of children. 该对象是父对象,内部有一系列子项。

Each child has an optional reference to the next child (the last child will have NextDestinationId = null) and an optional reference to the previous child (PreviousDestinationId should be null for the first child). 每个孩子都有对下一个孩子的可选引用(最后一个孩子的NextDestinationId = null)和对上一个孩子的可选引用(对于第一个孩子,PreviousDestinationId应该为null)。

在此处输入图片说明

Model Builder code: 模型制作器代码:

   modelBuilder.Entity<ScheduleItem>().HasOptional(t => t.NextDestination).WithMany().HasForeignKey(t => t.NextDestinationId);
        modelBuilder.Entity<ScheduleItem>().HasOptional(t => t.PreviousDestination).WithMany().HasForeignKey(t => t.PreviousDestinationId);

However, when saving I get this error unable to determine a valid ordering for dependent operations. 但是,保存时出现此错误, 无法确定相关操作的有效顺序。 Dependencies may exist due to foreign key constraints, model requirements, or store-generated values. 由于外键约束,模型要求或商店生成的值,可能存在依赖关系。

What's the solution to this problem? 这个问题有什么解决方案? do I have to call SaveChanges() twice? 我必须两次调用SaveChanges()吗?

The solution is to rethink the relationship and structure. 解决的办法是重新考虑关系和结构。 I have no comment on the parent/child relationship as you do not explain why a parent of the same type (a recursive relationship) is necessary. 我对父/子关系没有任何评论,因为您没有解释为什么必须使用相同类型的父(递归关系)。

The sibling relationship is not necessary, remove that all together and replace it with a SortOrder column of numeric type. 不需要同级关系,将它们全部删除,并用数字类型的SortOrder列替换。 The parent should then return a sorted list/collection based on this type. 然后,父级应基于此类型返回排序的列表/集合。 The schedule items should not have to know about the next/previous schedule items that are adjacent to them, let the parent worry about that. 进度项目不必知道与之相邻的下一个/上一个进度项目,让家长担心。

So replace 所以更换

public int? PreviousDestinationId { get; set; }
public int? NextDestinationId { get; set; }
public ScheduleItem PreviousDestination { get; set; }
public ScheduleItem NextDestination { get; set; }

with

// sort from low-to-high in the context of a collection
public int SortOrder { get; set; }

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

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