Skip to content

Commit

Permalink
Christmas crackers: Add Property/Reference/etc overloads that take IP…
Browse files Browse the repository at this point in the history
…roperty/INavigation (#27058)
  • Loading branch information
ajcvickers authored Jan 4, 2022
1 parent aefce2a commit 31b3ae4
Show file tree
Hide file tree
Showing 13 changed files with 486 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override object NextValue(EntityEntry entry)
continue;
}

var value = entry.Property(property.Name).CurrentValue;
var value = entry.Property(property).CurrentValue;

var converter = property.GetTypeMapping().Converter;
if (converter != null)
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/ChangeTracking/CollectionEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public CollectionEntry(InternalEntityEntry internalEntry, string name)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public CollectionEntry(InternalEntityEntry internalEntry, INavigation navigation)
: base(internalEntry, navigation)
public CollectionEntry(InternalEntityEntry internalEntry, INavigationBase navigationBase)
: base(internalEntry, navigationBase, collection: true)
{
LocalDetectChanges();
}
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore/ChangeTracking/CollectionEntry`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public CollectionEntry(InternalEntityEntry internalEntry, string name)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public CollectionEntry(InternalEntityEntry internalEntry, INavigation navigation)
: base(internalEntry, navigation)
public CollectionEntry(InternalEntityEntry internalEntry, INavigationBase navigationBase)
: base(internalEntry, navigationBase)
{
}

Expand Down
129 changes: 112 additions & 17 deletions src/EFCore/ChangeTracking/EntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,39 @@ public virtual DbContext Context
public virtual IEntityType Metadata
=> InternalEntry.EntityType;

/// <summary>
/// Provides access to change tracking information and operations for a given property or navigation of this entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
/// examples.
/// </remarks>
/// <param name="propertyBase">The property or navigation to access information and operations for.</param>
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
public virtual MemberEntry Member(IPropertyBase propertyBase)
{
Check.NotNull(propertyBase, nameof(propertyBase));

return propertyBase switch
{
IProperty property => new PropertyEntry(InternalEntry, property),
INavigationBase navigation => navigation.IsCollection
? new CollectionEntry(InternalEntry, navigation)
: new ReferenceEntry(InternalEntry, (INavigation)navigation),
_ => throw new InvalidOperationException(
CoreStrings.PropertyNotFound(propertyBase.Name, InternalEntry.EntityType.DisplayName()))
};
}

/// <summary>
/// Provides access to change tracking information and operations for a given
/// property or navigation property of this entity.
/// property or navigation of this entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
/// examples.
/// </remarks>
/// <param name="propertyName">The property to access information and operations for.</param>
/// <param name="propertyName">The property or navigation to access information and operations for.</param>
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
public virtual MemberEntry Member(string propertyName)
{
Expand All @@ -163,9 +187,8 @@ public virtual MemberEntry Member(string propertyName)
}

/// <summary>
/// Provides access to change tracking information and operations for all
/// properties and navigation properties of this entity.
/// </summary>
/// Provides access to change tracking information and operations for all properties and navigations of this entity.
/// </summary>-
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
/// examples.
Expand All @@ -174,15 +197,33 @@ public virtual IEnumerable<MemberEntry> Members
=> Properties.Cast<MemberEntry>().Concat(Navigations);

/// <summary>
/// Provides access to change tracking information and operations for a given
/// navigation property of this entity.
/// Provides access to change tracking information and operations for a given navigation of this entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyName">The property to access information and operations for.</param>
/// <param name="navigationBase">The navigation to access information and operations for.</param>
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
public virtual NavigationEntry Navigation(INavigationBase navigationBase)
{
Check.NotNull(navigationBase, nameof(navigationBase));

return navigationBase.IsCollection
? new CollectionEntry(InternalEntry, navigationBase)
: new ReferenceEntry(InternalEntry, (INavigation)navigationBase);
}

/// <summary>
/// Provides access to change tracking information and operations for a given navigation of this entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyName">The navigation to access information and operations for.</param>
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
public virtual NavigationEntry Navigation(string propertyName)
{
Expand Down Expand Up @@ -234,8 +275,23 @@ public virtual IEnumerable<NavigationEntry> Navigations
}

/// <summary>
/// Provides access to change tracking information and operations for a given
/// property of this entity.
/// Provides access to change tracking information and operations for a given property of this entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
/// examples.
/// </remarks>
/// <param name="property">The property to access information and operations for.</param>
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
public virtual PropertyEntry Property(IProperty property)
{
Check.NotNull(property, nameof(property));

return new PropertyEntry(InternalEntry, property);
}

/// <summary>
/// Provides access to change tracking information and operations for a given property of this entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
Expand Down Expand Up @@ -263,17 +319,36 @@ public virtual IEnumerable<PropertyEntry> Properties

/// <summary>
/// Provides access to change tracking and loading information for a reference (i.e. non-collection)
/// navigation property that associates this entity to another entity.
/// navigation that associates this entity to another entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyName">The name of the navigation property.</param>
/// <param name="navigation">The reference navigation.</param>
/// <returns>
/// An object that exposes change tracking information and operations for the
/// given navigation property.
/// An object that exposes change tracking information and operations for the given navigation.
/// </returns>
public virtual ReferenceEntry Reference(INavigationBase navigation)
{
Check.NotNull(navigation, nameof(navigation));

return new ReferenceEntry(InternalEntry, (INavigation)navigation);
}

/// <summary>
/// Provides access to change tracking and loading information for a reference (i.e. non-collection)
/// navigation that associates this entity to another entity.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyName">The name of the navigation.</param>
/// <returns>
/// An object that exposes change tracking information and operations for the given navigation.
/// </returns>
public virtual ReferenceEntry Reference(string propertyName)
{
Expand All @@ -297,17 +372,37 @@ public virtual IEnumerable<ReferenceEntry> References

/// <summary>
/// Provides access to change tracking and loading information for a collection
/// navigation property that associates this entity to a collection of another entities.
/// navigation that associates this entity to a collection of another entities.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
/// for more information and examples.
/// </remarks>
/// <param name="navigation">The collection navigation.</param>
/// <returns>
/// An object that exposes change tracking information and operations for the given navigation.
/// </returns>
public virtual CollectionEntry Collection(INavigationBase navigation)
{
Check.NotNull(navigation, nameof(navigation));

return new CollectionEntry(InternalEntry, navigation);
}

/// <summary>
/// Provides access to change tracking and loading information for a collection
/// navigation that associates this entity to a collection of another entities.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyName">The name of the navigation property.</param>
/// <param name="propertyName">The name of the navigation.</param>
/// <returns>
/// An object that exposes change tracking information and operations for the
/// given navigation property.
/// given navigation.
/// </returns>
public virtual CollectionEntry Collection(string propertyName)
{
Expand Down
Loading

0 comments on commit 31b3ae4

Please sign in to comment.