-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathNavigationEntry.cs
175 lines (161 loc) · 9.12 KB
/
NavigationEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
namespace Microsoft.EntityFrameworkCore.ChangeTracking;
/// <summary>
/// Provides access to change tracking and loading information for a navigation property
/// that associates this entity to one or more other entities.
/// </summary>
/// <remarks>
/// <para>
/// Instances of this class are returned from methods when using the <see cref="ChangeTracker" /> API and it is
/// not designed to be directly constructed in your application code.
/// </para>
/// <para>
/// 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-load-related-data">Loading related entities</see> for more information and examples.
/// </para>
/// </remarks>
public abstract class NavigationEntry : MemberEntry
{
/// <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>
[EntityFrameworkInternal]
protected NavigationEntry(InternalEntityEntry internalEntry, string name, bool collection)
: this(internalEntry, GetNavigation(internalEntry, name), collection)
{
}
/// <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>
[EntityFrameworkInternal]
protected NavigationEntry(InternalEntityEntry internalEntry, INavigationBase navigationBase, bool collection)
: base(internalEntry, navigationBase)
{
if (collection
&& !navigationBase.IsCollection)
{
throw new InvalidOperationException(
CoreStrings.CollectionIsReference(
navigationBase.Name, internalEntry.EntityType.DisplayName(),
nameof(ChangeTracking.EntityEntry.Collection), nameof(ChangeTracking.EntityEntry.Reference)));
}
if (!collection
&& navigationBase.IsCollection)
{
throw new InvalidOperationException(
CoreStrings.ReferenceIsCollection(
navigationBase.Name, internalEntry.EntityType.DisplayName(),
nameof(ChangeTracking.EntityEntry.Reference), nameof(ChangeTracking.EntityEntry.Collection)));
}
}
private static INavigationBase GetNavigation(InternalEntityEntry internalEntry, string name)
{
var navigation = (INavigationBase?)internalEntry.EntityType.FindNavigation(name)
?? internalEntry.EntityType.FindSkipNavigation(name);
if (navigation == null)
{
if (internalEntry.EntityType.FindProperty(name) != null)
{
throw new InvalidOperationException(
CoreStrings.NavigationIsProperty(
name, internalEntry.EntityType.DisplayName(),
nameof(ChangeTracking.EntityEntry.Reference), nameof(ChangeTracking.EntityEntry.Collection),
nameof(ChangeTracking.EntityEntry.Property)));
}
throw new InvalidOperationException(CoreStrings.PropertyNotFound(name, internalEntry.EntityType.DisplayName()));
}
return navigation;
}
/// <summary>
/// Loads the entities referenced by this navigation property, unless <see cref="NavigationEntry.IsLoaded" />
/// is already set to <see langword="true"/>.
/// </summary>
/// <remarks>
/// <para>
/// 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-load-related-data">Loading related entities</see> for more information and examples.
/// </para>
/// </remarks>
/// <param name="options">Options to control the way related entities are loaded.</param>
public abstract void Load(LoadOptions options = LoadOptions.None);
/// <summary>
/// Loads entities referenced by this navigation property, unless <see cref="NavigationEntry.IsLoaded" />
/// is already set to <see langword="true"/>.
/// </summary>
/// <remarks>
/// <para>
/// Multiple active operations on the same context instance are not supported. Use <see langword="await" /> to ensure
/// that any asynchronous operations have completed before calling another method on this context.
/// </para>
/// <para>
/// 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-load-related-data">Loading related entities</see> for more information and examples.
/// </para>
/// </remarks>
/// <param name="options">Options to control the way related entities are loaded.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous save operation.</returns>
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
public abstract Task LoadAsync(LoadOptions options = LoadOptions.None, CancellationToken cancellationToken = default);
/// <summary>
/// Returns the query that would be used by <see cref="Load" /> to load entities referenced by
/// this navigation property.
/// </summary>
/// <remarks>
/// <para>
/// The query can be composed over using LINQ to perform filtering, counting, etc. without
/// actually loading all entities from the database.
/// </para>
/// <para>
/// 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-load-related-data">Loading related entities</see> for more information and examples.
/// </para>
/// </remarks>
/// <returns>The query to load related entities.</returns>
public abstract IQueryable Query();
/// <summary>
/// Gets or sets a value indicating whether the entity or entities referenced by this navigation property
/// are known to be loaded.
/// </summary>
/// <remarks>
/// <para>
/// Loading entities from the database using
/// <see cref="EntityFrameworkQueryableExtensions.Include{TEntity,TProperty}" /> or
/// <see
/// cref="EntityFrameworkQueryableExtensions.ThenInclude{TEntity,TPreviousProperty,TProperty}(Microsoft.EntityFrameworkCore.Query.IIncludableQueryable{TEntity,System.Collections.Generic.IEnumerable{TPreviousProperty}},System.Linq.Expressions.Expression{System.Func{TPreviousProperty,TProperty}})" />
/// , <see cref="Load" />, or <see cref="LoadAsync" /> will set this flag. Subsequent calls to <see cref="Load" />
/// or <see cref="LoadAsync" /> will then be a no-op.
/// </para>
/// <para>
/// It is possible for IsLoaded to be false even if all related entities are loaded. This is because, depending on
/// how entities are loaded, it is not always possible to know for sure that all entities in a related collection
/// have been loaded. In such cases, calling <see cref="Load" /> or <see cref="LoadAsync" /> will ensure all
/// related entities are loaded and will set this flag to <see langword="true"/>.
/// </para>
/// <para>
/// 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-load-related-data">Loading related entities</see> for more information and examples.
/// </para>
/// </remarks>
/// <value>
/// <see langword="true" /> if all the related entities are loaded or the IsLoaded has been explicitly set to <see langword="true"/>.
/// </value>
public virtual bool IsLoaded
{
get => InternalEntry.IsLoaded(Metadata);
set => InternalEntry.SetIsLoaded(Metadata, value);
}
/// <summary>
/// Gets the metadata that describes the facets of this property and how it maps to the database.
/// </summary>
public new virtual INavigationBase Metadata
=> (INavigationBase)base.Metadata;
}