Unhandled Exception: System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved
See original GitHub issueI’m attempting to do this:
class Car
{
public int Id { get; set; }
public string Producer { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int? OwnerId { get; set; }
[ForeignKey("OwnerId")]
public Person Owner { get; set; }
}
// Can have many cars but one active car
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
[ForeignKey("ActiveCarId")]
public Car ActiveCar { get; set; }
[InverseProperty("Owner")]
public ICollection<Car> OwnedCars { get; set; }
}
//Usage
using (var dbContext = new AppDbContext())
{
var person = new Person()
{
Name = "Captain",
Surname = "Morgan",
ActiveCar = new Car()
{
Producer = "Super Cars",
Model = "XXX",
Year = 2018
}
};
person.ActiveCar.Owner = person;
dbContext.People.Add(person);
int numAffected = dbContext.SaveChanges();
if (numAffected == 0)
{
Console.WriteLine("Something went wrong during saving");
}
}
Results in:
Unhandled Exception: System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved: ‘Person [Added] <- Owner { ‘OwnerId’ } Car [Added] <- ActiveCar { ‘ActiveCarId’ } Person [Added]’.
Is this possible somehow?
Expected result:
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Unable to save changes because a circular dependency ...
I was able to fix this by deleting PersonID property in my AspNetUser class, and based my new setup from This Tutorial.
Read more >Lazily resolving services to fix circular dependencies in .NET ...
NET Core's built-in IoC container detects this, and throws a helpful exception: System.InvalidOperationException: A circular dependency was ...
Read more >Dealing With Circular Dependency Injection References
Your main problem there is you are breaking dependency injection by manually injecting dependencies. It does solve the DI blowing up, but IMO ......
Read more >Entity Framework circular dependency for last entity
I get the following error: Unable to save changes because a circular dependency was detected in the data to be saved: ''What' {'LastTrackId'}...
Read more >How to avoid circular references with EF Core and Global ...
One thing that is crucial to notice is that the circular dependency is between TenantSecurityService and the persistence layer, not between ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Save entities in multiple SaveChanges.
Duplicate of #1699