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

Use field/properties appropriately from query #16796

Merged
merged 1 commit into from
Jul 28, 2019
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 @@ -569,7 +569,8 @@ private static Expression AddToCollectionNavigation(
Expression.Constant(navigation.GetCollectionAccessor()),
_collectionAccessorAddMethodInfo,
entity,
relatedEntity);
relatedEntity,
Expression.Constant(true));

private static readonly MethodInfo _collectionAccessorAddMethodInfo
= typeof(IClrCollectionAccessor).GetTypeInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private static void InitializeIncludeCollection<TParent, TNavigationEntity>(
SetIsLoadedNoTracking(entity, navigation);
}

collection = clrCollectionAccessor.GetOrCreate(entity);
collection = clrCollectionAccessor.GetOrCreate(entity, forMaterialization: true);
}

var parentKey = parentIdentifier(queryContext, dbDataReader);
Expand Down Expand Up @@ -469,7 +469,7 @@ private static Expression AssignReferenceNavigation(
ParameterExpression relatedEntity,
INavigation navigation)
{
return entity.MakeMemberAccess(navigation.GetMemberInfo(forConstruction: false, forSet: true))
return entity.MakeMemberAccess(navigation.GetMemberInfo(forConstruction: true, forSet: true))
.CreateAssignExpression(relatedEntity);
}

Expand All @@ -482,7 +482,8 @@ private static Expression AddToCollectionNavigation(
Expression.Constant(navigation.GetCollectionAccessor()),
_collectionAccessorAddMethodInfo,
entity,
relatedEntity);
relatedEntity,
Expression.Constant(true));
}

private static readonly MethodInfo _collectionAccessorAddMethodInfo
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/CollectionEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public override IQueryable Query()
}

private void EnsureInitialized()
=> InternalEntry.GetOrCreateCollection(Metadata);
=> Metadata.AsNavigation().CollectionAccessor.GetOrCreate(InternalEntry.Entity, forMaterialization: true);

/// <summary>
/// The <see cref="EntityEntry" /> of an entity this navigation targets.
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/CollectionEntry`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public CollectionEntry([NotNull] InternalEntityEntry internalEntry, [NotNull] IN
/// </summary>
public new virtual IQueryable<TRelatedEntity> Query()
{
InternalEntry.GetOrCreateCollection(Metadata);
InternalEntry.GetOrCreateCollection(Metadata, forMaterialization: true);

return (IQueryable<TRelatedEntity>)base.Query();
}
Expand Down
40 changes: 27 additions & 13 deletions src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public virtual void SetPropertyModified(
SetOriginalValue(property, GetCurrentValue(property));
}

SetProperty(property, GetOriginalValue(property), setModified: false);
SetProperty(property, GetOriginalValue(property), isMaterialization: false, setModified: false);
}

_stateData.FlagProperty(propertyIndex, PropertyFlag.Modified, isModified);
Expand Down Expand Up @@ -603,7 +603,7 @@ public virtual void SetTemporaryValue([NotNull] IProperty property, object value
CoreStrings.TempValue(property.Name, EntityType.DisplayName()));
}

SetProperty(property, value, setModified, isCascadeDelete: false, CurrentValueType.Temporary);
SetProperty(property, value, isMaterialization: false, setModified, isCascadeDelete: false, CurrentValueType.Temporary);
}

/// <summary>
Expand All @@ -620,7 +620,7 @@ public virtual void SetStoreGeneratedValue(IProperty property, object value)
CoreStrings.StoreGenValue(property.Name, EntityType.DisplayName()));
}

SetProperty(property, value, setModified: true, isCascadeDelete: false, CurrentValueType.StoreGenerated);
SetProperty(property, value, isMaterialization: false, setModified: true, isCascadeDelete: false, CurrentValueType.StoreGenerated);
}

/// <summary>
Expand Down Expand Up @@ -745,11 +745,20 @@ protected virtual bool PropertyHasDefaultValue([NotNull] IPropertyBase propertyB
/// 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 virtual void WritePropertyValue([NotNull] IPropertyBase propertyBase, [CanBeNull] object value)
protected virtual void WritePropertyValue(
[NotNull] IPropertyBase propertyBase,
[CanBeNull] object value,
bool forMaterialization)
{
Debug.Assert(!propertyBase.IsShadowProperty());

((PropertyBase)propertyBase).Setter.SetClrValue(Entity, value);
var concretePropertyBase = (PropertyBase)propertyBase;

var setter = forMaterialization
? concretePropertyBase.MaterializationSetter
: concretePropertyBase.Setter;

setter.SetClrValue(Entity, value);
}

/// <summary>
Expand All @@ -758,11 +767,11 @@ protected virtual void WritePropertyValue([NotNull] 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>
public virtual object GetOrCreateCollection([NotNull] INavigation navigation)
public virtual object GetOrCreateCollection([NotNull] INavigation navigation, bool forMaterialization)
{
Debug.Assert(!navigation.IsShadowProperty());

return ((Navigation)navigation).CollectionAccessor.GetOrCreate(Entity);
return ((Navigation)navigation).CollectionAccessor.GetOrCreate(Entity, forMaterialization);
}

/// <summary>
Expand All @@ -784,11 +793,14 @@ public virtual bool CollectionContains([NotNull] INavigation navigation, [NotNul
/// 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 bool AddToCollection([NotNull] INavigation navigation, [NotNull] InternalEntityEntry value)
public virtual bool AddToCollection(
[NotNull] INavigation navigation,
[NotNull] InternalEntityEntry value,
bool forMaterialization)
{
Debug.Assert(!navigation.IsShadowProperty());

return ((Navigation)navigation).CollectionAccessor.Add(Entity, value.Entity);
return ((Navigation)navigation).CollectionAccessor.Add(Entity, value.Entity, forMaterialization);
}

/// <summary>
Expand Down Expand Up @@ -1039,7 +1051,7 @@ public object this[[NotNull] IPropertyBase propertyBase]
return value;
}

[param: CanBeNull] set => SetProperty(propertyBase, value);
[param: CanBeNull] set => SetProperty(propertyBase, value, isMaterialization: false);
}

/// <summary>
Expand All @@ -1051,13 +1063,15 @@ public object this[[NotNull] IPropertyBase propertyBase]
public virtual void SetProperty(
[NotNull] IPropertyBase propertyBase,
[CanBeNull] object value,
bool isMaterialization,
bool setModified = true,
bool isCascadeDelete = false)
=> SetProperty(propertyBase, value, setModified, isCascadeDelete, CurrentValueType.Normal);
=> SetProperty(propertyBase, value, isMaterialization, setModified, isCascadeDelete, CurrentValueType.Normal);

private void SetProperty(
[NotNull] IPropertyBase propertyBase,
[CanBeNull] object value,
bool isMaterialization,
bool setModified,
bool isCascadeDelete,
CurrentValueType valueType)
Expand Down Expand Up @@ -1138,7 +1152,7 @@ private void SetProperty(

if (valueType == CurrentValueType.Normal)
{
WritePropertyValue(propertyBase, value);
WritePropertyValue(propertyBase, value, isMaterialization);
}
else
{
Expand All @@ -1155,7 +1169,7 @@ private void SetProperty(
var defaultValue = asProperty.ClrType.GetDefaultValue();
if (!equals(currentValue, defaultValue))
{
WritePropertyValue(asProperty, defaultValue);
WritePropertyValue(asProperty, defaultValue, isMaterialization);
}

if (_storeGeneratedValues.TryGetValue(storeGeneratedIndex, out var generatedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ private static INotifyCollectionChanged AsINotifyCollectionChanged(
IEntityType entityType,
ChangeTrackingStrategy changeTrackingStrategy)
{
if (!(navigation.GetCollectionAccessor()?.GetOrCreate(entry.Entity) is INotifyCollectionChanged notifyingCollection))
if (!(navigation.GetCollectionAccessor()
?.GetOrCreate(entry.Entity, forMaterialization: false) is INotifyCollectionChanged notifyingCollection))
{
throw new InvalidOperationException(
CoreStrings.NonNotifyingCollection(navigation.Name, entityType.DisplayName(), changeTrackingStrategy));
Expand Down
12 changes: 6 additions & 6 deletions src/EFCore/ChangeTracking/Internal/InternalMixedEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ protected override bool PropertyHasDefaultValue(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 void WritePropertyValue(IPropertyBase propertyBase, object value)
protected override void WritePropertyValue(IPropertyBase propertyBase, object value, bool forMaterialization)
{
if (!propertyBase.IsShadowProperty())
{
base.WritePropertyValue(propertyBase, value);
base.WritePropertyValue(propertyBase, value, forMaterialization);
}
else
{
Expand All @@ -119,10 +119,10 @@ protected override void WritePropertyValue(IPropertyBase propertyBase, object va
/// 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 override object GetOrCreateCollection(INavigation navigation)
public override object GetOrCreateCollection(INavigation navigation, bool forMaterialization)
=> navigation.IsShadowProperty()
? GetOrCreateCollectionTyped(navigation)
: base.GetOrCreateCollection(navigation);
: base.GetOrCreateCollection(navigation,forMaterialization);

private ICollection<object> GetOrCreateCollectionTyped(INavigation navigation)
{
Expand Down Expand Up @@ -152,11 +152,11 @@ public override bool CollectionContains(INavigation navigation, InternalEntityEn
/// 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 override bool AddToCollection(INavigation navigation, InternalEntityEntry value)
public override bool AddToCollection(INavigation navigation, InternalEntityEntry value, bool forMaterialization)
{
if (!navigation.IsShadowProperty())
{
return base.AddToCollection(navigation, value);
return base.AddToCollection(navigation, value, forMaterialization);
}

if (navigation.GetTargetType().ClrType == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected override bool PropertyHasDefaultValue(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 void WritePropertyValue(IPropertyBase propertyBase, object value)
protected override void WritePropertyValue(IPropertyBase propertyBase, object value, bool forMaterialization)
=> _propertyValues[propertyBase.GetShadowIndex()] = value;

/// <summary>
Expand All @@ -102,7 +102,7 @@ protected override void WritePropertyValue(IPropertyBase propertyBase, object va
/// 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 override object GetOrCreateCollection(INavigation navigation)
public override object GetOrCreateCollection(INavigation navigation, bool forMaterialization)
=> GetOrCreateCollectionTyped(navigation);

private ICollection<object> GetOrCreateCollectionTyped(INavigation navigation)
Expand Down Expand Up @@ -131,7 +131,7 @@ public override bool CollectionContains(INavigation navigation, InternalEntityEn
/// 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 override bool AddToCollection(INavigation navigation, InternalEntityEntry value)
public override bool AddToCollection(INavigation navigation, InternalEntityEntry value, bool forMaterialization)
{
if (navigation.GetTargetType().ClrType == null)
{
Expand Down
Loading