-
Notifications
You must be signed in to change notification settings - Fork 3.3k
The change of a navigation to a new instance and removing the old instance throws an Exception #29356
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
Comments
Same root cause as #29318. |
@ajcvickers I´m not sure if that is correct.
But if I change the EF version back to |
@roji @AndriySvyryd This looks like a regression in the update pipeline where a temporary FK value is being sent instead of getting the new generated value and using that. (Similar to #28654.) Entities before saving are:
In EF7, we generate this:
In EF Core 6.0, we generate this:
Full code: #nullable enable
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
await using (MyDbContext dbContext = new())
{
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
dbContext.Add(
new Book
{
Author = new Author()
{
Name = "Alice",
AuthorsClub = new AuthorsClub()
{
Name = "AC South"
}
}
});
dbContext.SaveChanges();
}
await using (MyDbContext dbContext = new())
{
AuthorsClub authorsClubNorth = new()
{
Name = "AC North"
};
Author authorOfTheYear2023 = new()
{
Name = "Author of the year 2023",
AuthorsClub = authorsClubNorth
};
dbContext.Add(authorsClubNorth);
dbContext.Add(authorOfTheYear2023);
Book book = await dbContext
.Books
.Include(b => b.Author)
.SingleAsync();
Author authorOfTheYear2022 = book.Author!;
book.Author = authorOfTheYear2023;
dbContext.ChangeTracker.DetectChanges();
dbContext.Remove(authorOfTheYear2022);
// The authorsClubSouth should not be touched.
await dbContext.SaveChangesAsync(); // ❌
// Microsoft.EntityFrameworkCore.DbUpdateException -> SqlException
// The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Authors_AuthorsClub_AuthorsClubId".
// The conflict occurred in database "EfCore_TempKeySample_Sample", table "dbo.AuthorsClub", column 'Id'.
}
sealed class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Author>(b =>
{
b.HasOne(a => a.AuthorsClub)
.WithMany()
.HasForeignKey(a => a.AuthorsClubId);
});
modelBuilder.Entity<Book>(b =>
{
b.HasOne(book => book.Author)
.WithMany()
.HasForeignKey(book => book.AuthorId);
});
}
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
}
sealed class AuthorsClub
{
public int Id { get; set; }
public string? Name { get; set; }
}
sealed class Author
{
public int Id { get; set; }
public string? Name { get; set; }
public int AuthorsClubId { get; set; }
public AuthorsClub? AuthorsClub { get; set; }
}
sealed class Book
{
public int Id { get; set; }
public string? Title { get; set; }
public int AuthorId { get; set; }
public Author? Author { get; set; }
} |
Entities and DbContext
Setup
Generated SQL
Include provider and version information
EF Core version: 7.0 RC1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0 / .NET 7.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.3.5
The text was updated successfully, but these errors were encountered: