-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Adding new item in One-To-Many relationship throws exception within an One-To-One relationship #20984
Comments
@mirind4 This code is problematic: public WorkEntity()
{
QuotationEntity = new QuotationEntity();
} Since QuotationEntity is an entity type, this code creates a new child entity instance for every parent, even if there is already an existing entity in the database. See #18007 for more details. |
Hey @ajcvickers! Many thanks for your answer, I apprecaite it! I am not sure that it is the main cause what you mentioned. Let's say that I do not initialize the
At the last SaveChangesAsync() the same exception is thrown. What do you think? Thanks in advance! |
@mirind4 Guid key properties are configured to use generated values by default. This means that EF can use the key value to determine whether or not an entity instance represents a new or existing row in the database by checking if the key value has been set. However, in this code: DbContext.QuotationEntities.Add(new QuotationEntity() { WorkEntityId = workEntity.Id }); the key value is explicitly set even though the entity is new. This can be fixed in two ways:
The first approach is usually preferred. |
Hmm having similar issue with Guid FKs. An item has FK to another item in a one to many relationship and when adding new item to the primary items collection those new items are marked with Modified state - the change tracker says because the FK has changed. The FK property was of type Guid and obviously had the default value of Empty guid when class was instantiated. So I figured this is why EF was saying the FK changed and I went and changed this property to nullable, Guid?. Now when instantiating the property is null. It only gets its FK value assigned when calling .Add() on the main item collection. Yet the tracker still says that this new collection item's state is Modified. Because of this new entity being marked as Modfied, when calling SaveChangesAsync() a DbUpdateConcurrencyException is raised I believe because it is trying to find the entity in the database but it's not there. A TimeStamped Version property on the entity is at 0. Clearly this has to be a bug or particularly weird design? |
I am trying to add a new entity in a One-To-Many relationship. Normally this works, but in this case I have an One-To-One relationship, where the target entity has a collection of other entities which I want to populate. I am using SQLite in memory database and I want to keep using it.
To Reproduce
I made a simplified version of my problem from the production code in order to make it more understandable. I have a parent entity called
WorkEntity
. It has an one-to-one relationship with the entity calledQuotationEntity
. ThisQuotationEntity
has an One-To-Many relationship withQuotationLine
. So far so good. Let's see how the classes look like. First theWorkEntity
:Then the
QuotationEntity
Last but not least the
QuotationLine
:Let's see how my test code looks like and what the occurred exception is I am struggling with:
My test is fairly simple. First I create
WorkEntity
with its correspondingQuotationEntity
without noQuotationLine
. After persisting this entity, I retrieve it with the corresponding repository, then I try to add aQuotationLine
to theQuotationEntity
. When theDbContext.SaveChangesAsync()
is invoked at the second time, I got the following exception:Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation 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
WorkRepository
class looks like this:For configuring the One-To-One relationship between
WorkEntity
andQuotationEntity
I use the following code snippet:Last but not least, I use in memory SQLite database and EF Core 3.1 for reproducing this issue:
Since I have spent much time on trying to fix the issue, I have some findings:
One possible reason for this exception could be that child entities are not included when the
WorkEntity
is retrieved from the repository. If I would not include them, then it would be logical that the entity is not tracked by the EF ChangeTracker. But as you can see I include all the child entities.What I found out during debugging is that the
QuotationLine
has not got the correct state. If I set its state explicitly like:then everything works as expected. But I think it is not the solution, it is just a workaround. The EFCore should handle the ChangeTracking of the element, at least I would expect it from the framework. Or am I wrong?
Let me know then I can provide more code for my problem if needed. Thanks in advance for help!
Additional context
Microsoft.Data.Sqlite version: Microsoft.EntityFrameworkCore.Sqlite, Version=3.1.0.0
Target framework: 3.1
Operating system: Windows
The text was updated successfully, but these errors were encountered: