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

Erroneous circular dependency detection when updating entities with composite primary key and one-to-one relationships #29737

Closed
stanislav-a-frolov opened this issue Dec 2, 2022 · 2 comments

Comments

@stanislav-a-frolov
Copy link

Hello, everyone.

Having following model with composite primary key and optional one-to-one relationship with shared key part (Id2)

public class Model1
{
    public int Id1 { get; set; }
    public int Id2 { get; set; }
    public int? Model2Id1 { get; set; }
    public Model2? Model2 { get; set; }
}
public class Model2
{
    public int Id1 { get; set; }
    public int Id2 { get; set; }
    public Model1? Model1 { get; set; }
}

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions options) : base(options) {}

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Model1>(entity => entity.HasKey(entity => new { entity.Id1, entity.Id2 }));
        modelBuilder.Entity<Model2>(entity =>
        {
            entity.HasKey(entity => new { entity.Id1, entity.Id2 });
            entity.HasOne(e => e.Model1).WithOne(e => e.Model2).HasForeignKey<Model1>(e=> new { e.Model2Id1, e.Id2 }).IsRequired(false);
        });
    }
}

I perform following operations on empty db (note that Model2 is not referenced):

context.AddRange(
    new Model1 { Id2 = 1, Id1 = 1, Model2Id1 = null },
    new Model1 { Id2 = 1, Id1 = 2, Model2Id1 = null }
);
var models = new[] {
    new Model1 { Id2 = 2, Id1 = 1, Model2Id1 = null },
    new Model1 { Id2 = 2, Id1 = 2, Model2Id1 = null }
};
context.AddRange(models);
context.SaveChanges();
context.UpdateRange(models);
context.SaveChanges();

and get System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved
That's for ef core7, version 6 is fine.
Minimalistic project for repro is attached:
TestEf7.zip

@ajcvickers
Copy link
Member

/cc @AndriySvyryd

@AndriySvyryd
Copy link
Member

Duplicate of #29647

@AndriySvyryd AndriySvyryd marked this as a duplicate of #29647 Dec 8, 2022
@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Dec 8, 2022
@AndriySvyryd AndriySvyryd removed their assignment Dec 8, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Dec 9, 2022
@ajcvickers ajcvickers removed this from the 8.0.0 milestone Dec 9, 2022
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