-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Temporary Key is inserted as ForeignKey #31559
Labels
area-change-tracking
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
type-bug
Milestone
Comments
@roji @AndriySvyryd Looks like a bug in the update pipeline when inserting into a join table with an Identity column. Repro: using (var context = new SomeDbContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
var marques = new Author { Name = "Gabriel García Márquez" };
var goethe = new Author { Name = "Johann Wolfgang von Goethe" };
var hundred = new Book { Title = "One Hundred Years of Solitude" };
var faust = new Book { Title = "Faust" };
hundred.Authors.Add(marques);
faust.Authors.Add(goethe);
context.Books.Add(hundred);
context.Books.Add(faust);
context.SaveChanges();
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>()
.HasMany(author => author.Books)
.WithMany(book => book.Authors)
.UsingEntity<BookAuthor>();
}
public DbSet<Author> Authors { get; set; } = null!;
public DbSet<Book> Books { get; set; } = null!;
public DbSet<BookAuthor> BookAuthors { get; set; } = null!;
}
public class Book
{
public Guid Id { get; set; }
public string? Title { get; set; }
public ICollection<Author> Authors { get; set; } = new List<Author>();
}
public class BookAuthor
{
public int Id { get; set; }
public Guid BookId { get; set; }
public int AuthorId { get; set; }
}
public class Author
{
public int Id { get; set; }
public string? Name { get; set; }
public ICollection<Book> Books { get; set; } = new List<Book>();
} Stack trace:
|
AndriySvyryd
added
the
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
label
Sep 27, 2023
AndriySvyryd
added a commit
that referenced
this issue
Sep 27, 2023
AndriySvyryd
added a commit
that referenced
this issue
Sep 27, 2023
AndriySvyryd
added a commit
that referenced
this issue
Sep 27, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area-change-tracking
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
type-bug
I have a
Book
that has an n:m relationship withAuthor
. The primary key ofBook
is aGuid
and that ofAuthor
isint
, which is additionally defined as aColumnIdentity
. There is an intermediate classBookAuthor
with anint
primary keyId
, which is also defined asColumnIdentity
.DemoDbContext
looks like:Now when I try to add some data with the following code, I get a conflict with a foreign key constraint.
results in
If I set a breakpoint on
db.SaveChanges()
and remove the foreign key constraint in the database, theSaveChanges()
method succeeds. But in theBookAuthor
table, the foreign key values forAuthors
contain the negative temporary keys and not the positive final keys.I have attached a small solution that illustrates the problem: TempKeyBugExample.zip
The database tables look like:
Of course, you could add and save the authors beforehand. The following code works:
But shouldn't the first code mentioned work from scratch? Am I missing something?
provider and version information
EF Core version: 7.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 11 Pro
IDE: Visual Studio 2022 17.7.2
The text was updated successfully, but these errors were encountered: