Git Product home page Git Product logo

Comments (4)

JonathanMagnan avatar JonathanMagnan commented on May 26, 2024

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.

schuettecarsten avatar schuettecarsten commented on May 26, 2024

@JonathanMagnan

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.

schuettecarsten avatar schuettecarsten commented on May 26, 2024

@JonathanMagnan

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:

  1. Create a simple entity with FirstName value William
  2. Add the entity to the Change Tracker (line 23) using context.Add(entity)
  3. Use BulkMerge to add the entity to the database, check if a William Shakespeare already exists (lines 32 to 40)
  4. Update the entity property FirstName to Heinz
  5. Update the entity in the Change Tracker by calling context.Update(entity)
  6. 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.

JonathanMagnan avatar JonathanMagnan commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.