EF Core not tracking changes for Cosmos embedded entity
I have a model that looks similar to this (greatly simplified) ...
public class EntityA
{
public Guid Id { get; set; }
public string Title { get; set; }
public EntityB EmbeddedEntity { get; set; }
}
public class EntityB
{
public string EmbeddedTitle { get; set; }
}
I've set up the model configuration so that EntityB is embedded in EntityA ...
modelBuilder.Entity<EntityA>()
.OwnsOne(x => x.EmbeddedObject);
... which looks like this in Cosmos (minus all the auto-generated stuff) ...
{
"Id": "<guid>",
"Title": "Entity A title",
"EmbeddedEntity": {
"EmbeddedTitle": "Embedded Entity B title"
}
}
When I run a query to retrieve EntityA, then update the EmbeddedTitle property on EmbeddedEntity, similar to this code ...
{
...
var myEntity = await _context.MyEntities.FindAsync(entityId);
myEntity.EmbeddedEntity.EmbeddedTitle = "Some updated title";
await _context.SaveChangesAsync(cancellationToken);
...
}
... my changes are saved, but the EntityState for myObject is Unchanged. This is a problem because I'm trying to record when an entity was last modified based on the EntityState (I am overriding SaveChangesAsync so I can check EntityState, update a last modified property, and then call base.SaveChanges(cancellationToken)).
I can't work out if I'm missing something in my configuration, or if this is a bug in either EF Core or the Cosmos data provider (Microsoft.EntityFrameworkCore.Cosmos). No errors are being thrown so I'm leaning towards this being my problem.
I'm writing this with .NET 5.0 and EF Core 5.0.3
@Dazfl Can you add the following code before you call SaveChangesAsync?
context.ChangeTracker.DetectChanges();
Console.WriteLine(context.ChangeTracker.DebugView.LongView);
And then post the output here.
(Substitute Console.WriteLine with whatever works for you to get the text.)
@ajcvickers Apologies for the delayed reply. I think I worked out what is going on. It turns out that all I needed to execute was ...
_context.MyEntities.Update(myEntity);
... and then State had a status of Modified.
I'm still puzzled though ... in my case, how does the entity become disconnected? Especially when I'm retrieving an entity (FindAsync), modifying the entity and then saving the entity, all in one method?
Or maybe it isn't disconnected. I added in the code you suggested and a new EntityB was Added, the old EntityB was Deleted, but EntityA was Unchanged (sorry I don't have the proper output as my EntityA and EntityB are actually production code ... I don't have a test application running).
@Dazfl What you are seeing is that the change to the owned entity is being tracked, but this doesn't change the state of the owner. This is because EF tracks changes at a fine level of granularity so that the database provider can make the best choice for updating the database. For the case of Cosmos, the entire aggregate is updated, but for a relational database only the row for the owned entity might need updating, without touching the row for the owner.
Thanks @ajcvickers for getting back to me with an explanation of what is going on.
I ended up writing a little demo app to show the output you mentioned above. This is what is being tracked when I updated the embedded entity
EntityA {Id: 1} Unchanged
Id: 1 PK
Discriminator: 'EntityA'
Title: 'Entity A title'
__id: 'EntityA|1' AK
__jObject: '{
"Id": 1,
"Discriminator": "EntityA",
"Title": "En...'
EmbeddedEntity: {EntityAId: 1}
EntityB {EntityAId: 1} Modified
EntityAId: 1 PK FK
Title: 'Updated Entity B title' Modified Originally 'Entity B title'
__jObject: '{
"Title": "Entity B title"
}'
Hopefully that helps.
For now, simply calling context.EntityA.Update(myEntity); works as a work-around.
@AndriySvyryd Why is this a bug?
Initially I though that we could make this work as expected, but it would probably introduce too much complexity.
@AndriySvyryd I think it is reasonable to know whether or not an aggregate has been modified, but I think that would have to be a new API.