18

Is it possible to save model which has got circular dependency ?

I denormalized my Database:

User
--------
UserId
UserName
LastOrder   - fk to last order


Order
-------
OrderId
OrderName
UserId
OrderDate


Is it possible to save sth like this using entityFramework?

what should I change to make it work cause now I have got error:

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

7
  • Isn't the last order the first order matching the user id sorted by time descending? I think you could do without the FK if you had an index on user id and order date. Commented Nov 29, 2010 at 13:45
  • It is simplified, In my real app I want exactly that reference, is it possible to achieve ? Commented Nov 29, 2010 at 13:46
  • Yes LastOrder is OrderId Commented Nov 29, 2010 at 13:46
  • id have 1 FK reference or the other, I wouldn't have both as it seems counter intuitive Commented Nov 29, 2010 at 13:47
  • Order has got FK to user -> natural user has got fk to Order -> not natural but useful in my case. Is it possible to save sth like that ? thanks for help Commented Nov 29, 2010 at 13:53

1 Answer 1

49

I had a table that had a relationship to itself and i was getting the same error. Turns out the foreign key has to be nullable. Same table relationship in Entity Framework I hope this saves someone more time than I wasted.

Sign up to request clarification or add additional context in comments.

4 Comments

Saves me a lot of time also. Thanks! Does anyone knows why this happens? why does the foreign key must be nullable?
@Adam - I am guessing it is to allow you to insert the first row into the table. (there will be no other records to link to)
Thanks for the answer. I had to change my relationship from 1 to many to 0..1 to many to prevent a "Multiplicity is not valid" error after I set my foreign key to nullable but everything is running smoothly now.
Well, this won't work for circular references which is what was asked, but good to know nevertheless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.