You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that if I have related data which is stored as JSONB, updating the related data through another entity will not performed unless I set context.Entry(entity).Property(u => u.Metadata).IsModified = true;. As an example, consider the scenario where I have a Person that has multiple addresses, like this:
Creating a Person would work just fine. All data would be inserted and nothing would be incorrect. However, in the /update endpoint, if I try to update an Address.Metadata through a Person, the Metadata properties won't be updated in the database unless I explicitly (for each address) declare that the Metadata has changes: context.Entry(address).Property(u => u.Metadata).IsModified = true;. Why is this the case
app.MapPost("/add",(MyDbContextcontext)=>{Personperson=new(){Name="Person 1",Addresses=new(){new(){PersonId=1,Metadata=new(){HouseNumber=30,Door="N/A"},}}}; context.People.Add(person); context.SaveChanges();return Results.Ok();});
app.MapPost("/update",(MyDbContextcontext)=>{varperson= context.People.Include(x => x.Addresses).FirstOrDefault(x => x.Id ==1);foreach(var address in person.Addresses){ address.Metadata.HouseNumber =36; address.Metadata.Door ="John Doe";//context.Entry(address).Property(u => u.Metadata).IsModified = true;// Without the line above, the Metadata will remain as HouseNumber = 30, Door = "N/A" in the database even though they're changed here.} context.People.Update(person); context.SaveChanges();return Results.Ok();});
I am interested in knowing why this is the case. Is it intentional that I have to mark the address as modified so that EF Core can include it in its UPDATE SQL statement or is it a bug where it does not track changes when in reality it should? Why is the .Property(u => u.Metadata).IsModified = true; line needed explicitly?
The text was updated successfully, but these errors were encountered:
This has been previously raised in #1228 and #1168; I just opened #3314 to give a more thorough explanation of the situation and to track doing this for DOM JSON mapping (not POCO). To summarize, consider switching to EF's ToJson() support rather than using the older-style POCO mapping with jsonb - that would fix this.
I noticed that if I have related data which is stored as JSONB, updating the related data through another entity will not performed unless I set
context.Entry(entity).Property(u => u.Metadata).IsModified = true;
. As an example, consider the scenario where I have a Person that has multiple addresses, like this:With a DbContext setup such as this:
Creating a
Person
would work just fine. All data would be inserted and nothing would be incorrect. However, in the/update
endpoint, if I try to update anAddress.Metadata
through aPerson
, theMetadata
properties won't be updated in the database unless I explicitly (for each address) declare that the Metadata has changes:context.Entry(address).Property(u => u.Metadata).IsModified = true;
. Why is this the caseI am interested in knowing why this is the case. Is it intentional that I have to mark the address as modified so that EF Core can include it in its
UPDATE
SQL statement or is it a bug where it does not track changes when in reality it should? Why is the.Property(u => u.Metadata).IsModified = true;
line needed explicitly?The text was updated successfully, but these errors were encountered: