I have a object of type 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).
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?