Skip to content

Commit 31b3ae4

Browse files
authored
Christmas crackers: Add Property/Reference/etc overloads that take IProperty/INavigation (#27058)
1 parent aefce2a commit 31b3ae4

File tree

13 files changed

+486
-94
lines changed

13 files changed

+486
-94
lines changed

src/EFCore.Cosmos/ValueGeneration/Internal/IdValueGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected override object NextValue(EntityEntry entry)
6161
continue;
6262
}
6363

64-
var value = entry.Property(property.Name).CurrentValue;
64+
var value = entry.Property(property).CurrentValue;
6565

6666
var converter = property.GetTypeMapping().Converter;
6767
if (converter != null)

src/EFCore/ChangeTracking/CollectionEntry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public CollectionEntry(InternalEntityEntry internalEntry, string name)
4747
/// doing so can result in application failures when updating to a new Entity Framework Core release.
4848
/// </summary>
4949
[EntityFrameworkInternal]
50-
public CollectionEntry(InternalEntityEntry internalEntry, INavigation navigation)
51-
: base(internalEntry, navigation)
50+
public CollectionEntry(InternalEntityEntry internalEntry, INavigationBase navigationBase)
51+
: base(internalEntry, navigationBase, collection: true)
5252
{
5353
LocalDetectChanges();
5454
}

src/EFCore/ChangeTracking/CollectionEntry`.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public CollectionEntry(InternalEntityEntry internalEntry, string name)
4545
/// doing so can result in application failures when updating to a new Entity Framework Core release.
4646
/// </summary>
4747
[EntityFrameworkInternal]
48-
public CollectionEntry(InternalEntityEntry internalEntry, INavigation navigation)
49-
: base(internalEntry, navigation)
48+
public CollectionEntry(InternalEntityEntry internalEntry, INavigationBase navigationBase)
49+
: base(internalEntry, navigationBase)
5050
{
5151
}
5252

src/EFCore/ChangeTracking/EntityEntry.cs

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,39 @@ public virtual DbContext Context
129129
public virtual IEntityType Metadata
130130
=> InternalEntry.EntityType;
131131

132+
/// <summary>
133+
/// Provides access to change tracking information and operations for a given property or navigation of this entity.
134+
/// </summary>
135+
/// <remarks>
136+
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
137+
/// examples.
138+
/// </remarks>
139+
/// <param name="propertyBase">The property or navigation to access information and operations for.</param>
140+
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
141+
public virtual MemberEntry Member(IPropertyBase propertyBase)
142+
{
143+
Check.NotNull(propertyBase, nameof(propertyBase));
144+
145+
return propertyBase switch
146+
{
147+
IProperty property => new PropertyEntry(InternalEntry, property),
148+
INavigationBase navigation => navigation.IsCollection
149+
? new CollectionEntry(InternalEntry, navigation)
150+
: new ReferenceEntry(InternalEntry, (INavigation)navigation),
151+
_ => throw new InvalidOperationException(
152+
CoreStrings.PropertyNotFound(propertyBase.Name, InternalEntry.EntityType.DisplayName()))
153+
};
154+
}
155+
132156
/// <summary>
133157
/// Provides access to change tracking information and operations for a given
134-
/// property or navigation property of this entity.
158+
/// property or navigation of this entity.
135159
/// </summary>
136160
/// <remarks>
137161
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
138162
/// examples.
139163
/// </remarks>
140-
/// <param name="propertyName">The property to access information and operations for.</param>
164+
/// <param name="propertyName">The property or navigation to access information and operations for.</param>
141165
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
142166
public virtual MemberEntry Member(string propertyName)
143167
{
@@ -163,9 +187,8 @@ public virtual MemberEntry Member(string propertyName)
163187
}
164188

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

176199
/// <summary>
177-
/// Provides access to change tracking information and operations for a given
178-
/// navigation property of this entity.
200+
/// Provides access to change tracking information and operations for a given navigation of this entity.
179201
/// </summary>
180202
/// <remarks>
181203
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
182204
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
183205
/// for more information and examples.
184206
/// </remarks>
185-
/// <param name="propertyName">The property to access information and operations for.</param>
207+
/// <param name="navigationBase">The navigation to access information and operations for.</param>
208+
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
209+
public virtual NavigationEntry Navigation(INavigationBase navigationBase)
210+
{
211+
Check.NotNull(navigationBase, nameof(navigationBase));
212+
213+
return navigationBase.IsCollection
214+
? new CollectionEntry(InternalEntry, navigationBase)
215+
: new ReferenceEntry(InternalEntry, (INavigation)navigationBase);
216+
}
217+
218+
/// <summary>
219+
/// Provides access to change tracking information and operations for a given navigation of this entity.
220+
/// </summary>
221+
/// <remarks>
222+
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
223+
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
224+
/// for more information and examples.
225+
/// </remarks>
226+
/// <param name="propertyName">The navigation to access information and operations for.</param>
186227
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
187228
public virtual NavigationEntry Navigation(string propertyName)
188229
{
@@ -234,8 +275,23 @@ public virtual IEnumerable<NavigationEntry> Navigations
234275
}
235276

236277
/// <summary>
237-
/// Provides access to change tracking information and operations for a given
238-
/// property of this entity.
278+
/// Provides access to change tracking information and operations for a given property of this entity.
279+
/// </summary>
280+
/// <remarks>
281+
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
282+
/// examples.
283+
/// </remarks>
284+
/// <param name="property">The property to access information and operations for.</param>
285+
/// <returns>An object that exposes change tracking information and operations for the given property.</returns>
286+
public virtual PropertyEntry Property(IProperty property)
287+
{
288+
Check.NotNull(property, nameof(property));
289+
290+
return new PropertyEntry(InternalEntry, property);
291+
}
292+
293+
/// <summary>
294+
/// Provides access to change tracking information and operations for a given property of this entity.
239295
/// </summary>
240296
/// <remarks>
241297
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and
@@ -263,17 +319,36 @@ public virtual IEnumerable<PropertyEntry> Properties
263319

264320
/// <summary>
265321
/// Provides access to change tracking and loading information for a reference (i.e. non-collection)
266-
/// navigation property that associates this entity to another entity.
322+
/// navigation that associates this entity to another entity.
267323
/// </summary>
268324
/// <remarks>
269325
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
270326
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
271327
/// for more information and examples.
272328
/// </remarks>
273-
/// <param name="propertyName">The name of the navigation property.</param>
329+
/// <param name="navigation">The reference navigation.</param>
274330
/// <returns>
275-
/// An object that exposes change tracking information and operations for the
276-
/// given navigation property.
331+
/// An object that exposes change tracking information and operations for the given navigation.
332+
/// </returns>
333+
public virtual ReferenceEntry Reference(INavigationBase navigation)
334+
{
335+
Check.NotNull(navigation, nameof(navigation));
336+
337+
return new ReferenceEntry(InternalEntry, (INavigation)navigation);
338+
}
339+
340+
/// <summary>
341+
/// Provides access to change tracking and loading information for a reference (i.e. non-collection)
342+
/// navigation that associates this entity to another entity.
343+
/// </summary>
344+
/// <remarks>
345+
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
346+
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
347+
/// for more information and examples.
348+
/// </remarks>
349+
/// <param name="propertyName">The name of the navigation.</param>
350+
/// <returns>
351+
/// An object that exposes change tracking information and operations for the given navigation.
277352
/// </returns>
278353
public virtual ReferenceEntry Reference(string propertyName)
279354
{
@@ -297,17 +372,37 @@ public virtual IEnumerable<ReferenceEntry> References
297372

298373
/// <summary>
299374
/// Provides access to change tracking and loading information for a collection
300-
/// navigation property that associates this entity to a collection of another entities.
375+
/// navigation that associates this entity to a collection of another entities.
376+
/// </summary>
377+
/// <remarks>
378+
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
379+
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
380+
/// for more information and examples.
381+
/// </remarks>
382+
/// <param name="navigation">The collection navigation.</param>
383+
/// <returns>
384+
/// An object that exposes change tracking information and operations for the given navigation.
385+
/// </returns>
386+
public virtual CollectionEntry Collection(INavigationBase navigation)
387+
{
388+
Check.NotNull(navigation, nameof(navigation));
389+
390+
return new CollectionEntry(InternalEntry, navigation);
391+
}
392+
393+
/// <summary>
394+
/// Provides access to change tracking and loading information for a collection
395+
/// navigation that associates this entity to a collection of another entities.
301396
/// </summary>
302397
/// <remarks>
303398
/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see>
304399
/// and <see href="https://aka.ms/efcore-docs-changing-relationships">Changing foreign keys and navigations</see>
305400
/// for more information and examples.
306401
/// </remarks>
307-
/// <param name="propertyName">The name of the navigation property.</param>
402+
/// <param name="propertyName">The name of the navigation.</param>
308403
/// <returns>
309404
/// An object that exposes change tracking information and operations for the
310-
/// given navigation property.
405+
/// given navigation.
311406
/// </returns>
312407
public virtual CollectionEntry Collection(string propertyName)
313408
{

0 commit comments

Comments
 (0)