This article is a mirror article of machine translation, please click here to jump to the original article.

View: 49435|Reply: 1

[Source] EF Entities of append type "XXXX" fail because other...

[Copy link]
Posted on 5/12/2021 9:43:53 AM | | |
EF appends an entity of type "XXXX" that fails because another entity of the same type already has the same primary key value. When using the "Attach" method or setting the entity's state to "Unchanged" or "Modified"

Because other entities of the same type already have the same primary key value. This behavior can occur when using the "Attach" method or when setting the status of an entity to "Unchanged" or "Modified" if any entity in the drawing has a conflicting key value
This may be because some entities are new and have not yet received a database-generated key-value (new entity), in which case the graph is tracked using the "Add" method or the "Added" entity state, and then the state of the non-new entity is set to "Unchanged" or "Modified" accordingly.
In fact, this error is that when EF opens a transaction, there is already a data with the same primary key in the context of EF, and I personally encounter this problem in various situations:

Situation 1:

Modifying, iterating through the same data in a table (e.g., iterating the inventory quantity of the same item), this case should be the first time the context cache is not cleared

The code is as follows:


The loop here does not report an error the first time, and the second time it will be errored

Solution:


Situation 2:

The loop operation first adds a piece of data, and then modifies the data

The code is as follows:


Errors are also reported here, especially for new EFs or new entities

Solution:








Previous:Redis Benchmark performance test
Next:ASP.NET Detailed explanation of the Middleware middleware of Core (9).
Posted on 9/22/2021 8:55:57 PM |
Learn to learn...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com