You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My issue: I'm getting a foreign key constraint after performing this process:
Create a database context
Load objects
Dispose context, keep objects
Create new context, clear entire database, dispose context
Create new context
Add objects to context
Call BulkSaveChanges()
At this point I get a foreign key constraint error. From the debugger, I suspect what's happening here is that my child objects are being passed the foreign key to their parent (the 'one' object in a 'many to one') from the first time the objects were loaded from the database. When the objects are saved, the child cannot be saved pointing to parent with ID e.g. 1 because now the parent has ID e.g. 2 since it's the second time it's been saved to the database, and the database persists the 'next index' value.
I've tried assigning the reference navigation and the reference navigation ID to null before adding to the database, but this results in those values being populated by the parent object, with the old incorrect foreign key.
I've posted a slightly more complete description of this issue here on StackOverflow, but the 'workaround' is no longer working for me: I'm still getting a foreign-key error if I set the reference-navigation and reference-navigation-id to null before adding to the dbContext. This appears to be happening because, after I add everything to the dbContext, the child objects are re-assigned with the foreign key which becomes outdated when the parent object is actually saved to the database.
Do you think you could create a runnable project with the issue? It doesn’t need to be your project, just a new solution with the minimum code to reproduce the issue.
Make sure the project work when calling SaveChanges instead of BulkSaveChanges.
For us, by having a small project, it will be way easier to understand what is currently causing this issue.
My issue: I'm getting a foreign key constraint after performing this process:
At this point I get a foreign key constraint error. From the debugger, I suspect what's happening here is that my child objects are being passed the foreign key to their parent (the 'one' object in a 'many to one') from the first time the objects were loaded from the database. When the objects are saved, the child cannot be saved pointing to parent with ID e.g. 1 because now the parent has ID e.g. 2 since it's the second time it's been saved to the database, and the database persists the 'next index' value.
I've tried assigning the reference navigation and the reference navigation ID to null before adding to the database, but this results in those values being populated by the parent object, with the old incorrect foreign key.
I've posted a slightly more complete description of this issue here on StackOverflow, but the 'workaround' is no longer working for me: I'm still getting a foreign-key error if I set the reference-navigation and reference-navigation-id to null before adding to the dbContext. This appears to be happening because, after I add everything to the dbContext, the child objects are re-assigned with the foreign key which becomes outdated when the parent object is actually saved to the database.
The StackOverflow issue:
https://stackoverflow.com/questions/77743493/ef-core-why-are-the-foreign-keys-in-my-one-to-many-association-becoming-outdate
Am I doing something incorrectly here? Happy to provide more details like step-by-step literal key values or whatever would help.
I've realized this only happens with BulkSaveChanges(); using the default SaveChanges() function yields no foreign-key constraint violation.
Versions
EF Core version: 7.0.13
Database provider: Microsoft.EntityFrameworkCore.Sqlite.Core 7.0.13
Target framework: .NET 6.0
Entity Framework Extensions: 7.100.0.5
Operating system: Windows 10
IDE: Visual Studio 2022 17.8.3
The text was updated successfully, but these errors were encountered: