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

[5.0.0-rc2] Change ChangeTrackingStrategy from annotation to field #22531

Merged
merged 1 commit into from
Sep 14, 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
7 changes: 4 additions & 3 deletions src/EFCore/Extensions/ConventionEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -606,8 +607,7 @@ public static IConventionIndex RemoveIndex(
[NotNull] this IConventionEntityType entityType,
ChangeTrackingStrategy? changeTrackingStrategy,
bool fromDataAnnotation = false)
=> Check.NotNull(entityType, nameof(entityType)).AsEntityType()
.SetChangeTrackingStrategy(
=> ((EntityType)entityType).SetChangeTrackingStrategy(
changeTrackingStrategy,
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

Expand All @@ -616,8 +616,9 @@ public static IConventionIndex RemoveIndex(
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> The configuration source for <see cref="EntityTypeExtensions.GetChangeTrackingStrategy" />. </returns>
[DebuggerStepThrough]
public static ConfigurationSource? GetChangeTrackingStrategyConfigurationSource([NotNull] this IConventionEntityType entityType)
=> entityType.FindAnnotation(CoreAnnotationNames.ChangeTrackingStrategy)?.GetConfigurationSource();
=> ((EntityType)entityType).GetChangeTrackingStrategyConfigurationSource();

/// <summary>
/// Sets the LINQ expression filter automatically applied to queries for this entity type.
Expand Down
6 changes: 3 additions & 3 deletions src/EFCore/Extensions/ConventionModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ public static IReadOnlyList<IConventionEntityType> FindLeastDerivedEntityTypes(
[NotNull] this IConventionModel model,
ChangeTrackingStrategy? changeTrackingStrategy,
bool fromDataAnnotation = false)
=> Check.NotNull((Model)model, nameof(model))
.SetChangeTrackingStrategy(
=> ((Model)model).SetChangeTrackingStrategy(
changeTrackingStrategy,
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

Expand All @@ -202,8 +201,9 @@ public static IReadOnlyList<IConventionEntityType> FindLeastDerivedEntityTypes(
/// </summary>
/// <param name="model"> The model to find configuration source for. </param>
/// <returns> The configuration source for <see cref="ModelExtensions.GetChangeTrackingStrategy" />. </returns>
[DebuggerStepThrough]
public static ConfigurationSource? GetChangeTrackingStrategyConfigurationSource([NotNull] this IConventionModel model)
=> model.FindAnnotation(CoreAnnotationNames.ChangeTrackingStrategy)?.GetConfigurationSource();
=> ((Model)model).GetChangeTrackingStrategyConfigurationSource();

/// <summary>
/// Returns a value indicating whether the entity types using the given type should be configured
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore/Extensions/EntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,9 @@ public static IIndex FindIndex([NotNull] this IEntityType entityType, [NotNull]
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <returns> The change tracking strategy. </returns>
[DebuggerStepThrough]
public static ChangeTrackingStrategy GetChangeTrackingStrategy([NotNull] this IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

return (ChangeTrackingStrategy?)entityType[CoreAnnotationNames.ChangeTrackingStrategy]
?? entityType.Model.GetChangeTrackingStrategy();
}
=> ((EntityType)entityType).GetChangeTrackingStrategy();

/// <summary>
/// Gets the data stored in the model for the given entity type.
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore/Extensions/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ public static bool IsShared([NotNull] this IModel model, [NotNull] Type type)
/// <returns> The change tracking strategy. </returns>
[DebuggerStepThrough]
public static ChangeTrackingStrategy GetChangeTrackingStrategy([NotNull] this IModel model)
=> (ChangeTrackingStrategy?)Check.NotNull(model, nameof(model))[CoreAnnotationNames.ChangeTrackingStrategy]
?? ChangeTrackingStrategy.Snapshot;
=> ((Model)model).GetChangeTrackingStrategy();

/// <summary>
/// <para>
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore/Extensions/MutableEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,7 @@ public static IMutableIndex RemoveIndex(
public static void SetChangeTrackingStrategy(
[NotNull] this IMutableEntityType entityType,
ChangeTrackingStrategy? changeTrackingStrategy)
=> Check.NotNull(entityType, nameof(entityType)).AsEntityType()
.SetChangeTrackingStrategy(changeTrackingStrategy, ConfigurationSource.Explicit);
=> ((EntityType)entityType).SetChangeTrackingStrategy(changeTrackingStrategy, ConfigurationSource.Explicit);

/// <summary>
/// Sets the LINQ expression filter automatically applied to queries for this entity type.
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore/Extensions/MutableModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ public static void SetPropertyAccessMode(
public static void SetChangeTrackingStrategy(
[NotNull] this IMutableModel model,
ChangeTrackingStrategy? changeTrackingStrategy)
=> Check.NotNull((Model)model, nameof(model))
.SetChangeTrackingStrategy(changeTrackingStrategy, ConfigurationSource.Explicit);
=> ((Model)model).SetChangeTrackingStrategy(changeTrackingStrategy, ConfigurationSource.Explicit);

/// <summary>
/// Marks the given entity type as ignored, preventing conventions from adding a matching entity type to the model.
Expand Down
9 changes: 0 additions & 9 deletions src/EFCore/Metadata/Internal/CoreAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ public static class CoreAnnotationNames
/// </summary>
public const string NavigationAccessMode = "NavigationAccessMode";

/// <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 const string ChangeTrackingStrategy = "ChangeTrackingStrategy";

/// <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 Expand Up @@ -289,7 +281,6 @@ public static class CoreAnnotationNames
ValueGeneratorFactory,
PropertyAccessMode,
NavigationAccessMode,
ChangeTrackingStrategy,
OwnedTypes,
DiscriminatorProperty,
DiscriminatorMappingComplete,
Expand Down
29 changes: 27 additions & 2 deletions src/EFCore/Metadata/Internal/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ private readonly SortedDictionary<string, ServiceProperty> _serviceProperties
private Key _primaryKey;
private bool? _isKeyless;
private EntityType _baseType;
private ChangeTrackingStrategy? _changeTrackingStrategy;

private ConfigurationSource? _primaryKeyConfigurationSource;
private ConfigurationSource? _isKeylessConfigurationSource;
private ConfigurationSource? _baseTypeConfigurationSource;
private ConfigurationSource? _changeTrackingStrategyConfigurationSource;

// Warning: Never access these fields directly as access needs to be thread-safe
private PropertyCounts _counts;
Expand Down Expand Up @@ -3064,7 +3066,17 @@ public virtual void AddData([NotNull] IEnumerable<object> data)

#endregion

#region Annotations
#region Other

/// <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>
[DebuggerStepThrough]
public virtual ChangeTrackingStrategy GetChangeTrackingStrategy()
=> _changeTrackingStrategy ?? Model.GetChangeTrackingStrategy();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -3086,7 +3098,11 @@ public virtual void AddData([NotNull] IEnumerable<object> data)
}
}

this.SetOrRemoveAnnotation(CoreAnnotationNames.ChangeTrackingStrategy, changeTrackingStrategy, configurationSource);
_changeTrackingStrategy = changeTrackingStrategy;

_changeTrackingStrategyConfigurationSource = _changeTrackingStrategy == null
? (ConfigurationSource?)null
: configurationSource.Max(_changeTrackingStrategyConfigurationSource);

return changeTrackingStrategy;
}
Expand Down Expand Up @@ -3131,6 +3147,15 @@ public virtual string CheckChangeTrackingStrategy(ChangeTrackingStrategy value,
return null;
}

/// <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 virtual ConfigurationSource? GetChangeTrackingStrategyConfigurationSource()
=> _changeTrackingStrategyConfigurationSource;

/// <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
28 changes: 27 additions & 1 deletion src/EFCore/Metadata/Internal/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ private readonly Dictionary<string, ConfigurationSource> _ignoredTypeNames
new Dictionary<Type, ConfigurationSource> { { DefaultPropertyBagType, ConfigurationSource.Convention } };

private bool? _skipDetectChanges;
private ChangeTrackingStrategy? _changeTrackingStrategy;

private ConfigurationSource? _changeTrackingStrategyConfigurationSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -931,6 +934,16 @@ public virtual void AddShared([NotNull] Type type, ConfigurationSource configura
return propertyAccessMode;
}

/// <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>
[DebuggerStepThrough]
public virtual ChangeTrackingStrategy GetChangeTrackingStrategy()
=> _changeTrackingStrategy ?? ChangeTrackingStrategy.Snapshot;

/// <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 All @@ -941,11 +954,24 @@ public virtual void AddShared([NotNull] Type type, ConfigurationSource configura
ChangeTrackingStrategy? changeTrackingStrategy,
ConfigurationSource configurationSource)
{
this.SetOrRemoveAnnotation(CoreAnnotationNames.ChangeTrackingStrategy, changeTrackingStrategy, configurationSource);
_changeTrackingStrategy = changeTrackingStrategy;

_changeTrackingStrategyConfigurationSource = _changeTrackingStrategy == null
? (ConfigurationSource?)null
: configurationSource.Max(_changeTrackingStrategyConfigurationSource);

return changeTrackingStrategy;
}

/// <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 virtual ConfigurationSource? GetChangeTrackingStrategyConfigurationSource()
=> _changeTrackingStrategyConfigurationSource;

/// <summary>
/// Runs the conventions when an annotation was set or removed.
/// </summary>
Expand Down