Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to determine a valid ordering for dependent operations

Tags:

This is my model

public string Content { get; set; }  public Faq Reply { set; get; }  public int? ReplyId { get; set; }  public ICollection<Faq> Children { get; set; }  [ForeignKey("WriterId")] public virtual UserProfile Writer { get; set; }  public virtual int? WriterId { get; set; }  public Status Status { get; set; }  [ForeignKey("DepartmentId")] public virtual Department Department { get; set; }  public virtual int? DepartmentId { get; set; } 

And this is my error

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

like image 767
Tom Smith Avatar asked Jan 04 '13 07:01

Tom Smith


1 Answers

Another possible cause of this is if the foreign key property is set incorrectly.

For example, this can happen in the following scenario:

  1. DepartmentId is set to zero or any other value that isn't a valid foreign key.
  2. Department is either null or it is a Department object that contains a null value for its own DepartmentId property.
  3. This configuration will cause Entity Framework to fail because it tries to find a Department that has a primary key of zero, which probably won't exist.

I ran into this exception once when I mapped one object to another and I incorrectly set the foreign key to zero instead of setting it to null.

I can't say that this configuration is what is causing your exception without seeing the actual values of the properties, but it is one possibility.

like image 55
Toby Artisan Avatar answered Oct 21 '22 14:10

Toby Artisan