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

BulkSaveChanges results in Foreign Key Constraint Violation but SaveChanges works #569

Open
asfarley opened this issue Jan 12, 2024 · 3 comments
Assignees

Comments

@asfarley
Copy link

My issue: I'm getting a foreign key constraint after performing this process:

  1. Create a database context
  2. Load objects
  3. Dispose context, keep objects
  4. Create new context, clear entire database, dispose context
  5. Create new context
  6. Add objects to context
  7. 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.

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

@JonathanMagnan
Copy link
Member

Hello,

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.

Best Regards,

Jon

@asfarley
Copy link
Author

Here's a project showing the issue:

https://github.com/asfarley/EFExtensionsFKCVError

@JonathanMagnan JonathanMagnan self-assigned this Jan 15, 2024
@JonathanMagnan
Copy link
Member

Hello @asfarley ,

We will look at this issue more deeply. As indeed, there is an anomalies in our library here.

Meanwhile, here is a quick fix to force you to keep the identity value:

dbContext.BulkSaveChanges(options => options.InsertKeepIdentity = true);

Otherwise, the identity value is skipped, which will lead to the FK error.

Best Regards,

Jon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants