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 all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ private bool TryRewriteContainsEntity(Expression source, Expression item, out Ex
var propertyGetter = property.GetGetter();
foreach (var value in values)
{
propertyValueList.Add(propertyGetter.GetClrValue(value));
propertyValueList.Add(propertyGetter.GetStructuralTypeClrValue(value));
}

rewrittenSource = Expression.Constant(propertyValueList);
Expand Down Expand Up @@ -971,7 +971,7 @@ private Expression CreatePropertyAccessExpression(Expression target, IProperty p
{
case SqlConstantExpression sqlConstantExpression:
return Expression.Constant(
property.GetGetter().GetClrValue(sqlConstantExpression.Value), property.ClrType.MakeNullable());
property.GetGetter().GetStructuralTypeClrValue(sqlConstantExpression.Value!), property.ClrType.MakeNullable());

case SqlParameterExpression sqlParameterExpression
when sqlParameterExpression.Name.StartsWith(QueryCompilationContext.QueryParameterPrefix, StringComparison.Ordinal):
Expand Down Expand Up @@ -1002,7 +1002,7 @@ when memberInitExpression.Bindings.SingleOrDefault(
private static T ParameterValueExtractor<T>(QueryContext context, string baseParameterName, IProperty property)
{
var baseParameter = context.ParameterValues[baseParameterName];
return baseParameter == null ? (T)(object)null : (T)property.GetGetter().GetClrValue(baseParameter);
return baseParameter == null ? (T)(object)null : (T)property.GetGetter().GetStructuralTypeClrValue(baseParameter);
}

private static List<TProperty> ParameterListValueExtractor<TEntity, TProperty>(
Expand All @@ -1016,7 +1016,7 @@ private static List<TProperty> ParameterListValueExtractor<TEntity, TProperty>(
}

var getter = property.GetGetter();
return baseListParameter.Select(e => e != null ? (TProperty)getter.GetClrValue(e) : (TProperty)(object)null).ToList();
return baseListParameter.Select(e => e != null ? (TProperty)getter.GetStructuralTypeClrValue(e) : (TProperty)(object)null).ToList();
}

private static bool IsNullSqlConstantExpression(Expression expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ private bool TryRewriteContainsEntity(Expression? source, Expression item, [NotN
var propertyGetter = property.GetGetter();
foreach (var value in values)
{
propertyValueList.Add(propertyGetter.GetClrValue(value));
propertyValueList.Add(propertyGetter.GetStructuralTypeClrValue(value));
}

rewrittenSource = Expression.Constant(propertyValueList);
Expand Down Expand Up @@ -1435,7 +1435,7 @@ private Expression CreatePropertyAccessExpression(Expression target, IProperty p
return Expression.Constant(
constantExpression.Value is null
? null
: property.GetGetter().GetClrValue(constantExpression.Value),
: property.GetGetter().GetStructuralTypeClrValue(constantExpression.Value),
property.ClrType.MakeNullable());

case MethodCallExpression { Method.IsGenericMethod: true } methodCallExpression
Expand Down Expand Up @@ -1478,7 +1478,7 @@ when CanEvaluate(memberInitExpression):
private static T? ParameterValueExtractor<T>(QueryContext context, string baseParameterName, IProperty property)
{
var baseParameter = context.ParameterValues[baseParameterName];
return baseParameter == null ? (T?)(object?)null : (T?)property.GetGetter().GetClrValue(baseParameter);
return baseParameter == null ? (T?)(object?)null : (T?)property.GetGetter().GetStructuralTypeClrValue(baseParameter);
}

private static List<TProperty?>? ParameterListValueExtractor<TEntity, TProperty>(
Expand All @@ -1492,7 +1492,7 @@ when CanEvaluate(memberInitExpression):
}

var getter = property.GetGetter();
return baseListParameter.Select(e => e != null ? (TProperty?)getter.GetClrValue(e) : (TProperty?)(object?)null).ToList();
return baseListParameter.Select(e => e != null ? (TProperty?)getter.GetStructuralTypeClrValue(e) : (TProperty?)(object?)null).ToList();
}

private static ConstantExpression GetValue(Expression expression)
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ private bool TryRewriteContainsEntity(Expression source, Expression item, [NotNu
var propertyGetter = property.GetGetter();
foreach (var value in values)
{
propertyValueList.Add(propertyGetter.GetClrValue(value));
propertyValueList.Add(propertyGetter.GetStructuralTypeClrValue(value));
}

rewrittenSource = Expression.Constant(propertyValueList);
Expand Down Expand Up @@ -1815,7 +1815,7 @@ private Expression CreatePropertyAccessExpression(Expression target, IProperty p
return Expression.Constant(
sqlConstantExpression.Value is null
? null
: property.GetGetter().GetClrValue(sqlConstantExpression.Value),
: property.GetGetter().GetStructuralTypeClrValue(sqlConstantExpression.Value),
property.ClrType.MakeNullable());

case SqlParameterExpression sqlParameterExpression
Expand Down Expand Up @@ -1847,7 +1847,7 @@ when memberInitExpression.Bindings.SingleOrDefault(
private static T? ParameterValueExtractor<T>(QueryContext context, string baseParameterName, IProperty property)
{
var baseParameter = context.ParameterValues[baseParameterName];
return baseParameter == null ? (T?)(object?)null : (T?)property.GetGetter().GetClrValue(baseParameter);
return baseParameter == null ? (T?)(object?)null : (T?)property.GetGetter().GetStructuralTypeClrValue(baseParameter);
}

private static List<TProperty?>? ParameterListValueExtractor<TEntity, TProperty>(
Expand All @@ -1861,7 +1861,7 @@ when memberInitExpression.Bindings.SingleOrDefault(
}

var getter = property.GetGetter();
return baseListParameter.Select(e => e != null ? (TProperty?)getter.GetClrValue(e) : (TProperty?)(object?)null).ToList();
return baseListParameter.Select(e => e != null ? (TProperty?)getter.GetStructuralTypeClrValue(e) : (TProperty?)(object?)null).ToList();
}

private static bool CanEvaluate(Expression expression)
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
Loading
Loading