Skip to content

Commit

Permalink
Limit change tracking proxies to only changing _and_ changed strategies
Browse files Browse the repository at this point in the history
Fixes #21920
  • Loading branch information
ajcvickers committed Aug 27, 2020
1 parent a57d2e5 commit c072747
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 117 deletions.
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);
}
}

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.SkipChangeTrackingStrategyValidationAnnotation] == "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
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.SkipChangeTrackingStrategyValidationAnnotation] == "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. 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

0 comments on commit c072747

Please sign in to comment.