Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

circular dependency in entity framework

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.

like image 783
gruber Avatar asked Nov 29 '10 13:11

gruber


People also ask

How do you fix a circular dependency problem?

There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A , if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A , refactoring may be necessary.

What is meant by circular dependency?

In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.

How do I remove circular references in Entity Framework?

Notice the navigation property check boxes, you can deselect them if you don't want them to be generated. To solve your circular reference problem, make sure only one or none are checked, not both.

Is it okay to have circular dependency?

and, yes, cyclic dependencies are bad: They cause programs to include unnecessary functionality because things are dragged in which aren't needed. They make it a lot harder to test software. They make it a lot harder to reason about software.


1 Answers

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.

like image 80
tkerwood Avatar answered Sep 22 '22 16:09

tkerwood