Aggregated state for entries containing JSON columns.
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
Hi,
This issue is somewhat related to #33428. Not directly, but it's within the same scope.
Once the JSON support was introduced, deducing the modified state of the containing types is not trivial. I do understand that these are owned types, and as such they're designed as separate entities.
But, in essence, as a consumer, all we care about is whether the underlying row has changed or not. Yes, we can calculate the state ourselves (as shown below). But, I'm afraid there are some performance penalties. If it's done internally within DetectChanges perhaps some double work can be avoided? Then this state would be exposed as a property? I might be wrong, I don't know :) I just wanted to open a discussion on this topic. Thank you.
```csharp
public static bool IsModified(this EntityEntry entry)
{
if (entry.State == EntityState.Added || entry.State == EntityState.Deleted) return false;
if (entry.State == EntityState.Modified) return true;
foreach (var navigation in entry.Navigations)
{
if (navigation is ReferenceEntry referenceEntry)
{
if (referenceEntry.TargetEntry is null) continue;
// A reference entry, even if not JSON, still can be mapped to the same table.
// So, we'll check for owned types instead.
// This will include owned types that are mapped to different table too,
// but I have no solution for that. I can't make that distinction.
if (referenceEntry.TargetEntry.Metadata.IsOwned() &&
referenceEntry.TargetEntry.State != EntityState.Unchanged)
{
return true;
}
}
else if (navigation is CollectionEntry collection)
{
if (collection.CurrentValue is null) continue;
foreach (var item in collection.CurrentValue)
{
var itemEntry = collection.FindEntry(item);
if (itemEntry is null) continue;
if (itemEntry.Metadata.IsMappedToJson() &&
itemEntry.State != EntityState.Unchanged)
{
return true;
}
}
}
}
return false;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.