Skip to content

Limit change tracking proxies to only changing _and_ changed strategies #22278

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Aug 28, 2020
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 @@ -43,7 +43,7 @@ public virtual void ProcessModelInitialized(
{
modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.ChangingAndChangedNotifications);
#pragma warning disable EF1001 // Internal EF Core API usage.
modelBuilder.HasAnnotation(CoreAnnotationNames.SkipChangeTrackingStrategyValidationAnnotation, "true");
modelBuilder.HasAnnotation(CoreAnnotationNames.FullChangeTrackingNotificationsRequiredAnnotation, "true");
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}
Expand Down
58 changes: 12 additions & 46 deletions src/EFCore.Proxies/Proxies/Internal/ProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,14 @@ private Type[] GetInterfacesToProxy(

if (options.UseChangeTrackingProxies)
{
var changeTrackingStrategy = entityType.GetChangeTrackingStrategy();
switch (changeTrackingStrategy)
if (!_notifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
case ChangeTrackingStrategy.ChangedNotifications:

if (!_notifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
interfacesToProxy.Add(_notifyPropertyChangedInterface);
}

break;
case ChangeTrackingStrategy.ChangingAndChangedNotifications:
case ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues:

if (!_notifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
interfacesToProxy.Add(_notifyPropertyChangedInterface);
}

if (!_notifyPropertyChangingInterface.IsAssignableFrom(entityType.ClrType))
{
interfacesToProxy.Add(_notifyPropertyChangingInterface);
}
interfacesToProxy.Add(_notifyPropertyChangedInterface);
}

break;
if (!_notifyPropertyChangingInterface.IsAssignableFrom(entityType.ClrType))
{
interfacesToProxy.Add(_notifyPropertyChangingInterface);
Copy link
Member

Choose a reason for hiding this comment

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

If interfacesToProxy is empty we shouldn't create a proxy for change tracking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do this in another PR

}
}

Expand All @@ -199,31 +182,14 @@ private IInterceptor[] GetNotifyChangeInterceptors(

if (options.UseChangeTrackingProxies)
{
var changeTrackingStrategy = entityType.GetChangeTrackingStrategy();
switch (changeTrackingStrategy)
if (!_notifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
case ChangeTrackingStrategy.ChangedNotifications:

if (!_notifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
interceptors.Add(new PropertyChangedInterceptor(entityType, options.CheckEquality));
}

break;
case ChangeTrackingStrategy.ChangingAndChangedNotifications:
case ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues:

if (!_notifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
interceptors.Add(new PropertyChangedInterceptor(entityType, options.CheckEquality));
}

if (!_notifyPropertyChangingInterface.IsAssignableFrom(entityType.ClrType))
{
interceptors.Add(new PropertyChangingInterceptor(entityType, options.CheckEquality));
}
interceptors.Add(new PropertyChangedInterceptor(entityType, options.CheckEquality));
}

break;
if (!_notifyPropertyChangingInterface.IsAssignableFrom(entityType.ClrType))
{
interceptors.Add(new PropertyChangingInterceptor(entityType, options.CheckEquality));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/EFCore/Infrastructure/ModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,15 +687,15 @@ protected virtual void ValidateChangeTrackingStrategy(
{
Check.NotNull(model, nameof(model));

if ((string)model[CoreAnnotationNames.SkipChangeTrackingStrategyValidationAnnotation] != "true")
var requireFullNotifications = (string)model[CoreAnnotationNames.FullChangeTrackingNotificationsRequiredAnnotation] == "true";
foreach (var entityType in model.GetEntityTypes())
{
foreach (var entityType in model.GetEntityTypes())
var errorMessage = entityType.AsEntityType().CheckChangeTrackingStrategy(
entityType.GetChangeTrackingStrategy(), requireFullNotifications);

if (errorMessage != null)
{
var errorMessage = entityType.AsEntityType().CheckChangeTrackingStrategy(entityType.GetChangeTrackingStrategy());
if (errorMessage != null)
{
throw new InvalidOperationException(errorMessage);
}
throw new InvalidOperationException(errorMessage);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/Metadata/Internal/CoreAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public static class CoreAnnotationNames
/// 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 const string SkipChangeTrackingStrategyValidationAnnotation = "ModelValidator.SkipChangeTrackingStrategyValidation";
public const string FullChangeTrackingNotificationsRequiredAnnotation = "ModelValidator.FullChangeTrackingNotificationsRequired";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -307,7 +307,7 @@ public static class CoreAnnotationNames
AmbiguousNavigations,
DuplicateServiceProperties,
AmbiguousField,
SkipChangeTrackingStrategyValidationAnnotation
FullChangeTrackingNotificationsRequiredAnnotation
};
}
}
34 changes: 24 additions & 10 deletions src/EFCore/Metadata/Internal/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,8 @@ public virtual void AddData([NotNull] IEnumerable<object> data)
{
if (changeTrackingStrategy != null)
{
var errorMessage = CheckChangeTrackingStrategy(changeTrackingStrategy.Value);
var requireFullNotifications = (string)Model[CoreAnnotationNames.FullChangeTrackingNotificationsRequiredAnnotation] == "true";
var errorMessage = CheckChangeTrackingStrategy(changeTrackingStrategy.Value, requireFullNotifications);
if (errorMessage != null)
{
throw new InvalidOperationException(errorMessage);
Expand All @@ -3087,21 +3088,34 @@ public virtual void AddData([NotNull] IEnumerable<object> data)
/// 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 virtual string CheckChangeTrackingStrategy(ChangeTrackingStrategy value)
public virtual string CheckChangeTrackingStrategy(ChangeTrackingStrategy value, bool requireFullNotifications)
{
if (ClrType != null)
{
if (value != ChangeTrackingStrategy.Snapshot
&& !typeof(INotifyPropertyChanged).IsAssignableFrom(ClrType))
if (requireFullNotifications)
{
return CoreStrings.ChangeTrackingInterfaceMissing(this.DisplayName(), value, nameof(INotifyPropertyChanged));
if (value != ChangeTrackingStrategy.ChangingAndChangedNotifications
&& value != ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues)
{
return CoreStrings.FullChangeTrackingRequired(
this.DisplayName(), value, nameof(ChangeTrackingStrategy.ChangingAndChangedNotifications),
nameof(ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues));
}
}

if ((value == ChangeTrackingStrategy.ChangingAndChangedNotifications
|| value == ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues)
&& !typeof(INotifyPropertyChanging).IsAssignableFrom(ClrType))
else
{
return CoreStrings.ChangeTrackingInterfaceMissing(this.DisplayName(), value, nameof(INotifyPropertyChanging));
if (value != ChangeTrackingStrategy.Snapshot
&& !typeof(INotifyPropertyChanged).IsAssignableFrom(ClrType))
{
return CoreStrings.ChangeTrackingInterfaceMissing(this.DisplayName(), value, nameof(INotifyPropertyChanged));
}

if ((value == ChangeTrackingStrategy.ChangingAndChangedNotifications
|| value == ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues)
&& !typeof(INotifyPropertyChanging).IsAssignableFrom(ClrType))
{
return CoreStrings.ChangeTrackingInterfaceMissing(this.DisplayName(), value, nameof(INotifyPropertyChanging));
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 30 additions & 27 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema

Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple

There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -483,6 +483,9 @@
<data name="ForeignKeyWrongType" xml:space="preserve">
<value>The foreign key {foreignKey} targeting the key {key} on '{principalType}' cannot be removed from the entity type '{entityType}' because it is defined on the entity type '{otherEntityType}'.</value>
</data>
<data name="FullChangeTrackingRequired" xml:space="preserve">
<value>The entity type '{entityType}' is configured to use the '{changeTrackingStrategy}' change tracking strategy when full change tracking notifications are required. Use 'ModelBuilder.HasChangeTrackingStrategy' in 'OnModelCreating' to configure all entity types in the model to use the '{fullStrategy}' or '{fullPlusStrategy}' strategy.</value>
</data>
<data name="FunctionOnClient" xml:space="preserve">
<value>The '{methodName}' method is not supported because the query has switched to client-evaluation. Inspect the log to determine which query expressions are triggering client-evaluation.</value>
</data>
Expand Down
Loading