Comments (4)
Hello @schuettecarsten ,
We tried to reproduce it. but unfortunately, we failed. Either modifying some existing entities/adding some and changing the state after still working. We are missing the model on the configuration about this bit
property that will help us to reproduce it.
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.
Best Regards,
Jon
from entityframework-extensions.
Sorry for confusion about the "bits", it is not a property in my model, I was talking about the internal property flags in Entity Framework, look at this file for details: https://github.com/dotnet/efcore/blob/release/8.0/src/EFCore/ChangeTracking/Internal/InternalEntityEntry.StateData.cs.
The state of the property value is set to "Temporary" and not reset by the Bulk operations if they read the identity value from the database and write it back to the entity. I am working on a small test project to demonstrate the issue.
from entityframework-extensions.
I've created a demo project for the issue. You can download it here, it needs some explanation:
https://1drv.ms/u/s!Ajk93YqKTO5fiJBfRF7Ri67u-YVmpg?e=j06GRG
Please make sure to create the database by running `Update-Database" as migrations are note automatically executed on startup. When you run the project, it will throw an exception in line 69.
The code does the following:
- Create a simple entity with FirstName value
William
- Add the entity to the Change Tracker (line 23) using
context.Add(entity)
- Use BulkMerge to add the entity to the database, check if a
William Shakespeare
already exists (lines 32 to 40) - Update the entity property FirstName to
Heinz
- Update the entity in the Change Tracker by calling
context.Update(entity)
- SaveChanges
The code produces the following output and exception:
Author.ID before Add: 0
Author.ID after Add: 0
EntityEntry state after Add: Added
Author.ID from EntityEntry values after Add: -2147482647
Author.ID after Merge: 1
EntityEntry state after Merge: Added
Author.ID from EntityEntry values after Merge: 1
Author.ID after State manipulation: 1
EntityEntry state after State manipulation: Added
Author.ID from EntityEntry values after State manipulation: 1
Author.ID after Update: 1
EntityEntry state after Update: Modified
Author.ID from EntityEntry values after Update: 1
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:
The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
The problem is that the entity state is not updated by BulkMerge
. The ID is set to the identity value from the database, but the EntityState
stays on Added
. Because of this, context.Update()
does not change the state to Modified
. SaveChanges
will try to add the entity again, this fails. So I need to set the EntityState
to Unmodified
after BulkMerge
.
But there is another issue with BulkMerge.
Run the code again on the same database. This time, it will throw an exception in line 60:
Author.ID before Add: 0
Author.ID after Add: 0
EntityEntry state after Add: Added
Author.ID from EntityEntry values after Add: -2147482647
Author.ID after Merge: 0
EntityEntry state after Merge: Added
Author.ID from EntityEntry values after Merge: -2147482647
Author.ID after State manipulation: 0
EntityEntry state after State manipulation: Added
Author.ID from EntityEntry values after State manipulation: -2147482647
System.InvalidOperationException:
The property 'Author.AuthorId' has a temporary value while attempting to change the entity's state to 'Unchanged'. Either set a permanent value explicitly, or ensure that the database is configured to generate values for this property.
For some reasons, and I have no idea why, the BulkMerge
call did not read the Author.ID value from the existing entity in the database as it did when the entity was inserted. I was not able to make my test code to update the IDs from the database for existing entities that match die merge key expression. On my other project, this works but I get some other exceptions. So we first need to make the ID update work for existing entities.
from entityframework-extensions.
Hello @schuettecarsten ,
Indeed, in your case, you currently use all your properties in your custom key. So the BulkMerge has nothing to UPDATE
. It only expected to be able to INSERT
rows.
In this specific case, you will need to use ForceOutputFromUnmodifiedRow
such as:
context.BulkMerge(entities, operation => {
operation.ForceOutputFromUnmodifiedRow = true;
});
So the Identity
will be also outputted for existing rows and ensure the value for the Id
is no longer a temporary value.
Let me know if that explains correctly the cause and solution for this issue.
Best Regards,
Jon
from entityframework-extensions.
Related Issues (20)
- Still missing column after updating database with Migration HOT 2
- Missing values in target table HOT 2
- Bulk synchronize problem with not null columns HOT 4
- MySql XAER_RMFAIL error with BulkSaveChanges and TransactionScope HOT 3
- 7.22.4 has undocumented breaking change in temporal table queries HOT 3
- InsertFromQuery ERROR: You currently try to map an entity type directly. You need to specify the property of this entity. HOT 2
- A failed bulk insert or update does not (always) tell which entities failed HOT 1
- ExecuteFutureAction (with useTransactions = true) swallows exceptions and can throw "The underlying provider failed on Rollback." exception HOT 2
- Npgsql.PostgresException: 23505: duplicate key value violates unique constraint HOT 6
- EF CORE 8 and Pgsql 8: Parameter without name in synchronize & merge HOT 3
- BulkInsert Error after upgrading to Ef Core .Net 8 with latest Extensions HOT 2
- BulkInsert Exception using NpgSql and jsonb in .NET 8 HOT 11
- Trial for EF core 7 HOT 3
- BulkInsert taking 20+ min for 80 000 data rows HOT 3
- Force output does not work for merge. HOT 3
- Invalid Cast. The update expression must be of type MemberInitExpression. HOT 3
- Bulk Operations for Postgres does not respect the PermanentTable setting HOT 2
- Avoid Prepared Statements for Bulk operations HOT 6
- WhereBulkContains throwing System.ArgumentNullException: 'Value cannot be null. (Parameter 'name')' HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entityframework-extensions.