-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
#28065 raised a 7.0 SaveChanges circular dependency issue with unique indexes; two repros were posted there - one with a filtered index, one without. The fix for that issue (#28923) seems to have fixed only the filtered index case, but the unfiltered one still fails on 7.0.
Repro
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
var db = new TestDbContext();
await db.Database.EnsureDeletedAsync();
await db.Database.EnsureCreatedAsync();
var t1 = new TestEntity();
db.Tests.Add(t1);
await db.SaveChangesAsync();
var dependent = new TestDependentEntity()
{
TestEntityId = t1.Id,
UniqueOn = null,
ToChange = 1,
};
var dependent2 = new TestDependentEntity()
{
TestEntityId = t1.Id,
UniqueOn = null,
ToChange = 2,
};
db.TestDependents.Add(dependent);
db.TestDependents.Add(dependent2);
await db.SaveChangesAsync();
dependent.ToChange = null;
dependent2.ToChange = null;
await db.SaveChangesAsync();
public class TestDbContext : DbContext
{
public DbSet<TestEntity> Tests { get; set; }
public DbSet<TestDependentEntity> TestDependents { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlite(@"Data Source=foo.sqlite")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TestDependentEntity>().HasIndex(e => new { e.TestEntityId, e.UniqueOn })
.IsUnique();
}
}
public class TestEntity
{
public int Id { get; set; }
}
public class TestDependentEntity
{
public int Id { get; set; }
public int TestEntityId { get; set; }
public bool? UniqueOn { get; set; }
public int? ToChange { get; set; }
}
/cc @AndriySvyryd
vslee, jmalczak, jgilchrist, Shaddix, dm-reznik and 2 more