Skip to content
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

Initial change tracking and DetectChanges for complex types #31453

Merged
merged 5 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/EFCore.InMemory/Storage/Internal/InMemoryTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using Microsoft.EntityFrameworkCore.InMemory.Internal;
using Microsoft.EntityFrameworkCore.InMemory.ValueGeneration.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.InMemory.Storage.Internal;

Expand Down Expand Up @@ -44,7 +45,7 @@ public InMemoryTable(
_sensitiveLoggingEnabled = sensitiveLoggingEnabled;
_nullabilityCheckEnabled = nullabilityCheckEnabled;
_rows = new Dictionary<TKey, object?[]>(_keyValueFactory.EqualityComparer);
var properties = entityType.GetProperties().ToList();
var properties = entityType.GetFlattenedProperties().ToList();
_propertyCount = properties.Count;

foreach (var property in properties)
Expand Down Expand Up @@ -163,15 +164,15 @@ private static List<ValueComparer> GetKeyComparers(IEnumerable<IProperty> proper
/// </summary>
public virtual void Create(IUpdateEntry entry, IDiagnosticsLogger<DbLoggerCategory.Update> updateLogger)
{
var properties = entry.EntityType.GetProperties().ToList();
var properties = entry.EntityType.GetFlattenedProperties().ToList();
var row = new object?[properties.Count];
var nullabilityErrors = new List<IProperty>();

for (var index = 0; index < properties.Count; index++)
{
var propertyValue = SnapshotValue(properties[index], properties[index].GetKeyValueComparer(), entry);

row[index] = propertyValue;
row[properties[index].GetIndex()] = propertyValue;
HasNullabilityError(properties[index], propertyValue, nullabilityErrors);
}

Expand All @@ -197,12 +198,12 @@ public virtual void Delete(IUpdateEntry entry, IDiagnosticsLogger<DbLoggerCatego

if (_rows.TryGetValue(key, out var row))
{
var properties = entry.EntityType.GetProperties().ToList();
var properties = entry.EntityType.GetFlattenedProperties().ToList();
var concurrencyConflicts = new Dictionary<IProperty, object?>();

for (var index = 0; index < properties.Count; index++)
{
IsConcurrencyConflict(entry, properties[index], row[index], concurrencyConflicts);
IsConcurrencyConflict(entry, properties[index], row[properties[index].GetIndex()], concurrencyConflicts);
}

if (concurrencyConflicts.Count > 0)
Expand Down Expand Up @@ -266,27 +267,28 @@ public virtual void Update(IUpdateEntry entry, IDiagnosticsLogger<DbLoggerCatego

if (_rows.TryGetValue(key, out var row))
{
var properties = entry.EntityType.GetProperties().ToList();
var properties = entry.EntityType.GetFlattenedProperties().ToList();
var comparers = GetKeyComparers(properties);
var valueBuffer = new object?[properties.Count];
var concurrencyConflicts = new Dictionary<IProperty, object?>();
var nullabilityErrors = new List<IProperty>();

for (var index = 0; index < valueBuffer.Length; index++)
{
if (IsConcurrencyConflict(entry, properties[index], row[index], concurrencyConflicts))
var propertyIndex = properties[index].GetIndex();
if (IsConcurrencyConflict(entry, properties[index], row[propertyIndex], concurrencyConflicts))
{
continue;
}

if (HasNullabilityError(properties[index], row[index], nullabilityErrors))
if (HasNullabilityError(properties[index], row[propertyIndex], nullabilityErrors))
{
continue;
}

valueBuffer[index] = entry.IsModified(properties[index])
? SnapshotValue(properties[index], comparers[index], entry)
: row[index];
: row[propertyIndex];
}

if (concurrencyConflicts.Count > 0)
Expand Down
59 changes: 21 additions & 38 deletions src/EFCore.Relational/Update/ColumnModification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,14 @@ public virtual object? Value
}
}

#pragma warning disable EF1001 // Internal EF Core API usage.
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static object? GetOriginalValue(IUpdateEntry entry, IProperty property)
=> GetEntry((IInternalEntry)entry, property).GetOriginalValue(property);
=> entry.GetOriginalValue(property);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -195,10 +194,10 @@ public virtual object? Value
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static object? GetOriginalProviderValue(IUpdateEntry entry, IProperty property)
=> GetEntry((IInternalEntry)entry, property).GetOriginalProviderValue(property);
=> entry.GetOriginalProviderValue(property);

private void SetOriginalValue(object? value)
=> GetEntry((IInternalEntry)Entry!, Property!).SetOriginalValue(Property!, value);
=> Entry!.SetOriginalValue(Property!, value);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -207,7 +206,7 @@ private void SetOriginalValue(object? value)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static object? GetCurrentValue(IUpdateEntry entry, IProperty property)
=> GetEntry((IInternalEntry)entry, property).GetCurrentValue(property);
=> entry.GetCurrentValue(property);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -216,7 +215,7 @@ private void SetOriginalValue(object? value)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static object? GetCurrentProviderValue(IUpdateEntry entry, IProperty property)
=> GetEntry((IInternalEntry)entry, property).GetCurrentProviderValue(property);
=> entry.GetCurrentProviderValue(property);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -225,7 +224,7 @@ private void SetOriginalValue(object? value)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static void SetStoreGeneratedValue(IUpdateEntry entry, IProperty property, object? value)
=> GetEntry((IInternalEntry)entry, property).SetStoreGeneratedValue(property, value);
=> entry.SetStoreGeneratedValue(property, value);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -234,7 +233,7 @@ public static void SetStoreGeneratedValue(IUpdateEntry entry, IProperty property
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static bool IsModified(IUpdateEntry entry, IProperty property)
=> GetEntry((IInternalEntry)entry, property).IsModified(property);
=> entry.IsModified(property);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -243,19 +242,7 @@ public static bool IsModified(IUpdateEntry entry, IProperty property)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static bool IsStoreGenerated(IUpdateEntry entry, IProperty property)
=> GetEntry((IInternalEntry)entry, property).IsStoreGenerated(property);

private static IInternalEntry GetEntry(IInternalEntry entry, IPropertyBase property)
{
if (property.DeclaringType.IsAssignableFrom(entry.StructuralType))
{
return entry;
}

var complexProperty = ((IComplexType)property.DeclaringType).ComplexProperty;
return GetEntry(entry, complexProperty).GetComplexPropertyEntry(complexProperty);
}
#pragma warning restore EF1001 // Internal EF Core API usage.
=> entry.IsStoreGenerated(property);

/// <inheritdoc />
public virtual string? JsonPath { get; }
Expand All @@ -275,30 +262,28 @@ public virtual void AddSharedColumnModification(IColumnModification modification
GetCurrentProviderValue(Entry, Property),
GetCurrentProviderValue(modification.Entry, modification.Property)))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
var existingEntry = GetEntry((IInternalEntry)Entry!, Property);
var newEntry = GetEntry((IInternalEntry)modification.Entry, modification.Property);
var existingEntry = Entry;
var newEntry = modification.Entry;

if (_sensitiveLoggingEnabled)
{
throw new InvalidOperationException(
RelationalStrings.ConflictingRowValuesSensitive(
existingEntry.StructuralType.DisplayName(),
newEntry.StructuralType.DisplayName(),
existingEntry.EntityType.DisplayName(),
newEntry.EntityType.DisplayName(),
Entry.BuildCurrentValuesString(Entry.EntityType.FindPrimaryKey()!.Properties),
GetEntry((IInternalEntry)Entry!, Property).BuildCurrentValuesString(new[] { Property }),
Entry.BuildCurrentValuesString(new[] { Property }),
newEntry.BuildCurrentValuesString(new[] { modification.Property }),
ColumnName));
}

throw new InvalidOperationException(
RelationalStrings.ConflictingRowValues(
existingEntry.StructuralType.DisplayName(),
newEntry.StructuralType.DisplayName(),
existingEntry.EntityType.DisplayName(),
newEntry.EntityType.DisplayName(),
new[] { Property }.Format(),
new[] { modification.Property }.Format(),
ColumnName));
#pragma warning restore EF1001 // Internal EF Core API usage.
}

if (UseOriginalValueParameter)
Expand Down Expand Up @@ -331,15 +316,14 @@ public virtual void AddSharedColumnModification(IColumnModification modification
}
else
{
#pragma warning disable EF1001 // Internal EF Core API usage.
var existingEntry = GetEntry((IInternalEntry)Entry!, Property);
var newEntry = GetEntry((IInternalEntry)modification.Entry, modification.Property);
var existingEntry = Entry;
var newEntry = modification.Entry;
if (_sensitiveLoggingEnabled)
{
throw new InvalidOperationException(
RelationalStrings.ConflictingOriginalRowValuesSensitive(
existingEntry.StructuralType.DisplayName(),
newEntry.StructuralType.DisplayName(),
existingEntry.EntityType.DisplayName(),
newEntry.EntityType.DisplayName(),
Entry.BuildCurrentValuesString(Entry.EntityType.FindPrimaryKey()!.Properties),
existingEntry.BuildOriginalValuesString(new[] { Property }),
newEntry.BuildOriginalValuesString(new[] { modification.Property }),
Expand All @@ -348,12 +332,11 @@ public virtual void AddSharedColumnModification(IColumnModification modification

throw new InvalidOperationException(
RelationalStrings.ConflictingOriginalRowValues(
existingEntry.StructuralType.DisplayName(),
newEntry.StructuralType.DisplayName(),
existingEntry.EntityType.DisplayName(),
newEntry.EntityType.DisplayName(),
new[] { Property }.Format(),
new[] { modification.Property }.Format(),
ColumnName));
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/EFCore.Relational/Update/ModificationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ void HandleSharedColumns(
result.Path.Insert(0, pathEntry);
}

var modifiedMembers = entry.EntityType.GetProperties().Where(entry.IsModified).ToList();
var modifiedMembers = entry.EntityType.GetFlattenedProperties().Where(entry.IsModified).ToList();
if (modifiedMembers.Count == 1)
{
result.Property = modifiedMembers[0];
Expand Down Expand Up @@ -854,7 +854,7 @@ private void WriteJson(
#pragma warning restore EF1001 // Internal EF Core API usage.

writer.WriteStartObject();
foreach (var property in entityType.GetProperties())
foreach (var property in entityType.GetFlattenedProperties())
{
if (property.IsKey())
{
Expand Down Expand Up @@ -1108,7 +1108,7 @@ public void RecordValue(IColumnMapping mapping, IUpdateEntry entry)
{
_currentValue = Update.ColumnModification.GetCurrentProviderValue(entry, property);
}

_write = !_originalValueInitialized
|| !mapping.Column.ProviderValueComparer.Equals(_originalValue, _currentValue);

Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/Internal/ChangeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private bool LocalDetectChanges(InternalEntityEntry entry)

OnDetectingEntityChanges(entry);

foreach (var property in entityType.GetProperties())
foreach (var property in entityType.GetFlattenedProperties())
{
if (property.GetOriginalValueIndex() >= 0
&& !entry.IsModified(property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ protected override int GetPropertyIndex(IPropertyBase propertyBase)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override int GetPropertyCount(IRuntimeTypeBase typeBase)
=> typeBase.ShadowPropertyCount;
protected override int GetPropertyCount(IRuntimeEntityType entityType)
=> entityType.ShadowPropertyCount;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
10 changes: 1 addition & 9 deletions src/EFCore/ChangeTracking/Internal/IInternalEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IInternalEntry
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
IRuntimeTypeBase StructuralType { get; }
IRuntimeEntityType EntityType { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is because even when we update only properties within a complex type, we still update the table represented by its containing entity type right? It seems to make sense, but I'm guessing @AndriySvyryd had something in mind here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complex objects aren't tracked directly, so they never have an entry in the state manager. Therefore, this can only ever be an entity type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect most of this interface to go away to support collections and value types, so any changes to it meanwhile are fine by me


/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -296,14 +296,6 @@ void SetEntityState(
bool acceptChanges = false,
bool modifyProperties = true);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
IInternalEntry GetComplexPropertyEntry(IComplexProperty property);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down

This file was deleted.

Loading