question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled Exception: System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved

See original GitHub issue

I’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:

image image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
smitpatelcommented, May 3, 2018

Save entities in multiple SaveChanges.

2reactions
ajcvickerscommented, May 3, 2018

Duplicate of #1699

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found