Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to cascade delete when circular dependency is in context #34076

Closed
nargatte opened this issue Jun 24, 2024 · 1 comment
Closed

Unable to cascade delete when circular dependency is in context #34076

nargatte opened this issue Jun 24, 2024 · 1 comment

Comments

@nargatte
Copy link

I have 2 entities: root (Foo) and chidden (Bar) that can be in circular dependency between each other. Tring to delete root case exception:

Unable to save changes because a circular dependency was detected in the data to be saved: 'Bar [Deleted] ForeignKeyConstraint { 'BarId' } <-
Bar [Deleted] ForeignKeyConstraint { 'BarId' } <-
Bar [Deleted]

Minimal reproducible example:

    internal class Context : DbContext
    {
        public Context()
        { }

        public DbSet<Foo> Foos { get; set; }
        public DbSet<Bar> Bars { get; set; }

        protected override void OnConfiguring(
            DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseNpgsql(
                "--secret--");
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Foo>()
                .HasKey(p => p.Id);

            modelBuilder.Entity<Bar>()
                .HasKey(p => p.Id);

            modelBuilder.Entity<Foo>()
                .HasMany(p => p.Bars)
                .WithOne(p => p.Foo)
                .HasForeignKey(x => x.FooId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<Bar>()
                .HasOne(p => p.Bar2)
                .WithOne()
                .OnDelete(DeleteBehavior.SetNull);
        }
    }

    public class Foo
    {
        public Guid Id { get; set; }

        public ICollection<Bar> Bars { get; set; }
    }

    public class Bar
    {
        public Guid Id { get; set; }
        public Guid FooId { get; set; }
        public Foo Foo { get; set; }

        public Guid? BarId { get; set; }
        public Bar? Bar2 { get; set; }
    }

Main:

var context = new Context();
var fooId = Guid.NewGuid();
var bar1Id = Guid.NewGuid();
var bar2Id = Guid.NewGuid();

context.Foos.Add(new Foo()
{
    Id = fooId,
    Bars = new[]
    {
        new Bar() {Id = bar1Id},
        new Bar() {Id = bar2Id}
    }
});

context.SaveChanges();

var b1 = context.Bars.FirstOrDefault(b => b.Id == bar1Id);
var b2 = context.Bars.FirstOrDefault(b => b.Id == bar2Id);

b1.Bar2 = b2;
b2.Bar2 = b1;

context.SaveChanges();

var f = context.Foos.Include(f => f.Bars).FirstOrDefault(f => f.Id == fooId);
context.Foos.Remove(f);
context.SaveChanges();

If Bars is not included or will be detached it works, database is able to cascade delete Bars itself.
I expect that EF will delete circular dependencies in this case in the same way as datebase.

Include provider and version information

EF Core version: 8.0.6
Database provider: (Npgsql.EntityFrameworkCore.PostgreSQL 8.0.4)
Target framework: (.NET 8.0)
Operating system: Windows 10
IDE: (eVisual Studio 2022 17.4)

@ajcvickers
Copy link
Member

Duplicate of #1699

@ajcvickers ajcvickers marked this as a duplicate of #1699 Jul 1, 2024
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
@ajcvickers ajcvickers removed their assignment Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants