Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

entity framework - Unable to determine a valid ordering for dependent operations

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...