Skip to content

Commit

Permalink
Fix for 6674. Clean-up. Removing the old way of configuring navigatio…
Browse files Browse the repository at this point in the history
…ns. New way already in place. (#20317)
  • Loading branch information
lajones authored Mar 17, 2020
1 parent 0573af8 commit cd883db
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 631 deletions.
36 changes: 8 additions & 28 deletions src/EFCore/Metadata/Builders/CollectionNavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,13 @@ public CollectionNavigationBuilder(
/// The name of the reference navigation property on the other end of this relationship.
/// If null or not specified, then there is no navigation property on the other end of the relationship.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object to further configure the relationship. </returns>
public virtual ReferenceCollectionBuilder WithOne(
[CanBeNull] string navigationName = null,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
public virtual ReferenceCollectionBuilder WithOne([CanBeNull] string navigationName = null)
=> new ReferenceCollectionBuilder(
DeclaringEntityType,
RelatedEntityType,
WithOneBuilder(
Check.NullButNotEmpty(navigationName, nameof(navigationName)),
navigationConfiguration).Metadata);
Check.NullButNotEmpty(navigationName, nameof(navigationName))).Metadata);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -142,10 +136,8 @@ public virtual ReferenceCollectionBuilder WithOne(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
protected virtual InternalRelationshipBuilder WithOneBuilder(
[CanBeNull] string navigationName,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
=> WithOneBuilder(MemberIdentity.Create(navigationName), navigationConfiguration);
protected virtual InternalRelationshipBuilder WithOneBuilder([CanBeNull] string navigationName)
=> WithOneBuilder(MemberIdentity.Create(navigationName));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -155,13 +147,10 @@ protected virtual InternalRelationshipBuilder WithOneBuilder(
/// </summary>
[EntityFrameworkInternal]
protected virtual InternalRelationshipBuilder WithOneBuilder(
[CanBeNull] MemberInfo navigationMemberInfo,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
=> WithOneBuilder(MemberIdentity.Create(navigationMemberInfo), navigationConfiguration);
[CanBeNull] MemberInfo navigationMemberInfo)
=> WithOneBuilder(MemberIdentity.Create(navigationMemberInfo));

private InternalRelationshipBuilder WithOneBuilder(
MemberIdentity reference,
Action<NavigationBuilder> navigationConfiguration = null)
private InternalRelationshipBuilder WithOneBuilder(MemberIdentity reference)
{
if (SkipNavigation != null)
{
Expand All @@ -187,7 +176,7 @@ private InternalRelationshipBuilder WithOneBuilder(
InternalRelationshipBuilder.ThrowForConflictingNavigation(foreignKey, referenceName, newToPrincipal: true);
}

var withOneBuilder = reference.MemberInfo == null || CollectionMember == null
return reference.MemberInfo == null || CollectionMember == null
? Builder.HasNavigations(
reference.Name, CollectionName,
(EntityType)DeclaringEntityType, (EntityType)RelatedEntityType,
Expand All @@ -196,15 +185,6 @@ private InternalRelationshipBuilder WithOneBuilder(
reference.MemberInfo, CollectionMember,
(EntityType)DeclaringEntityType, (EntityType)RelatedEntityType,
ConfigurationSource.Explicit);

if (navigationConfiguration != null
&& withOneBuilder.Metadata.DependentToPrincipal != null)
{
navigationConfiguration(
new NavigationBuilder(withOneBuilder.Metadata.DependentToPrincipal));
}

return withOneBuilder;
}

/// <summary>
Expand Down
19 changes: 4 additions & 15 deletions src/EFCore/Metadata/Builders/CollectionNavigationBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,15 @@ public CollectionNavigationBuilder(
/// The name of the reference navigation property on the other end of this relationship.
/// If null, there is no navigation property on the other end of the relationship.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object to further configure the relationship. </returns>
public new virtual ReferenceCollectionBuilder<TEntity, TRelatedEntity> WithOne(
[CanBeNull] string navigationName = null,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
[CanBeNull] string navigationName = null)
{
return new ReferenceCollectionBuilder<TEntity, TRelatedEntity>(
DeclaringEntityType,
RelatedEntityType,
WithOneBuilder(
Check.NullButNotEmpty(navigationName, nameof(navigationName)),
navigationConfiguration).Metadata);
Check.NullButNotEmpty(navigationName, nameof(navigationName))).Metadata);
}

/// <summary>
Expand All @@ -81,19 +76,13 @@ public CollectionNavigationBuilder(
/// relationship (<c>post => post.Blog</c>). If no property is specified, the relationship will be
/// configured without a navigation property on the other end of the relationship.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object to further configure the relationship. </returns>
public virtual ReferenceCollectionBuilder<TEntity, TRelatedEntity> WithOne(
[CanBeNull] Expression<Func<TRelatedEntity, TEntity>> navigationExpression,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
[CanBeNull] Expression<Func<TRelatedEntity, TEntity>> navigationExpression)
=> new ReferenceCollectionBuilder<TEntity, TRelatedEntity>(
DeclaringEntityType,
RelatedEntityType,
WithOneBuilder(
navigationExpression?.GetPropertyAccess(),
navigationConfiguration).Metadata);
WithOneBuilder(navigationExpression?.GetPropertyAccess()).Metadata);

/// <summary>
/// Configures this as a many-to-many relationship.
Expand Down
85 changes: 17 additions & 68 deletions src/EFCore/Metadata/Builders/EntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,21 +588,17 @@ private OwnedNavigationBuilder OwnsManyBuilder(in TypeIdentity ownedType, string
/// no property is specified, the relationship will be configured without a navigation property on this
/// end.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object that can be used to configure the relationship. </returns>
public virtual ReferenceNavigationBuilder HasOne(
[NotNull] string relatedTypeName,
[CanBeNull] string navigationName,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
[CanBeNull] string navigationName)
{
Check.NotEmpty(relatedTypeName, nameof(relatedTypeName));
Check.NullButNotEmpty(navigationName, nameof(navigationName));

var relatedEntityType = FindRelatedEntityType(relatedTypeName, navigationName);
var foreignKey = HasOneBuilder(
MemberIdentity.Create(navigationName), relatedEntityType, navigationConfiguration);
MemberIdentity.Create(navigationName), relatedEntityType);

return new ReferenceNavigationBuilder(
Builder.Metadata,
Expand Down Expand Up @@ -635,21 +631,17 @@ public virtual ReferenceNavigationBuilder HasOne(
/// no property is specified, the relationship will be configured without a navigation property on this
/// end.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object that can be used to configure the relationship. </returns>
public virtual ReferenceNavigationBuilder HasOne(
[NotNull] Type relatedType,
[CanBeNull] string navigationName = null,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
[CanBeNull] string navigationName = null)
{
Check.NotNull(relatedType, nameof(relatedType));
Check.NullButNotEmpty(navigationName, nameof(navigationName));

var relatedEntityType = FindRelatedEntityType(relatedType, navigationName);
var foreignKey = HasOneBuilder(
MemberIdentity.Create(navigationName), relatedEntityType, navigationConfiguration);
MemberIdentity.Create(navigationName), relatedEntityType);

return new ReferenceNavigationBuilder(
Builder.Metadata,
Expand All @@ -675,19 +667,14 @@ public virtual ReferenceNavigationBuilder HasOne(
/// The name of the reference navigation property on this entity type that represents
/// the relationship. The navigation must be a CLR property on the entity type.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object that can be used to configure the relationship. </returns>
public virtual ReferenceNavigationBuilder HasOne(
[NotNull] string navigationName,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
public virtual ReferenceNavigationBuilder HasOne([NotNull] string navigationName)
{
Check.NotEmpty(navigationName, nameof(navigationName));

return Metadata.ClrType == null
? HasOne(navigationName, null, navigationConfiguration) // Path only used by pre 3.0 snapshots
: HasOne(Metadata.GetNavigationMemberInfo(navigationName).GetMemberType(), navigationName, navigationConfiguration);
? HasOne(navigationName, null) // Path only used by pre 3.0 snapshots
: HasOne(Metadata.GetNavigationMemberInfo(navigationName).GetMemberType(), navigationName);
}

/// <summary>
Expand All @@ -699,8 +686,7 @@ public virtual ReferenceNavigationBuilder HasOne(
[EntityFrameworkInternal]
protected virtual ForeignKey HasOneBuilder(
MemberIdentity navigationId,
[NotNull] EntityType relatedEntityType,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
[NotNull] EntityType relatedEntityType)
{
ForeignKey foreignKey;
if (navigationId.MemberInfo != null)
Expand All @@ -716,19 +702,6 @@ protected virtual ForeignKey HasOneBuilder(
targetIsPrincipal: Builder.Metadata == relatedEntityType ? true : (bool?)null).Metadata;
}

if (navigationConfiguration != null
&& navigationId.Name != null)
{
var navigation =
Builder.Metadata == relatedEntityType
|| foreignKey.PrincipalEntityType == relatedEntityType
? foreignKey.DependentToPrincipal
: foreignKey.PrincipalToDependent;

navigationConfiguration(
new NavigationBuilder(navigation));
}

return foreignKey;
}

Expand All @@ -750,21 +723,16 @@ protected virtual ForeignKey HasOneBuilder(
/// no property is specified, the relationship will be configured without a navigation property on this
/// end.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object that can be used to configure the relationship. </returns>
public virtual CollectionNavigationBuilder HasMany(
[NotNull] string relatedTypeName,
[CanBeNull] string navigationName,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
[CanBeNull] string navigationName)
{
Check.NotEmpty(relatedTypeName, nameof(relatedTypeName));
Check.NullButNotEmpty(navigationName, nameof(navigationName));

return HasMany(navigationName,
FindRelatedEntityType(relatedTypeName, navigationName),
navigationConfiguration);
FindRelatedEntityType(relatedTypeName, navigationName));
}

/// <summary>
Expand All @@ -783,19 +751,14 @@ public virtual CollectionNavigationBuilder HasMany(
/// The name of the collection navigation property on this entity type that represents the relationship.
/// The navigation must be a CLR property on the entity type.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object that can be used to configure the relationship. </returns>
public virtual CollectionNavigationBuilder HasMany(
[NotNull] string navigationName,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
public virtual CollectionNavigationBuilder HasMany([NotNull] string navigationName)
{
Check.NotEmpty(navigationName, nameof(navigationName));

if (Metadata.ClrType == null)
{
return HasMany(navigationName, (string)null, navigationConfiguration);
return HasMany(navigationName, (string)null);
}

var memberType = Metadata.GetNavigationMemberInfo(navigationName).GetMemberType();
Expand All @@ -811,7 +774,7 @@ public virtual CollectionNavigationBuilder HasMany(
"T"));
}

return HasMany(elementType, navigationName, navigationConfiguration);
return HasMany(elementType, navigationName);
}

/// <summary>
Expand All @@ -837,27 +800,21 @@ public virtual CollectionNavigationBuilder HasMany(
/// no property is specified, the relationship will be configured without a navigation property on this
/// end.
/// </param>
/// <param name="navigationConfiguration">
/// An optional action which further configures the navigation property.
/// </param>
/// <returns> An object that can be used to configure the relationship. </returns>
public virtual CollectionNavigationBuilder HasMany(
[NotNull] Type relatedType,
[CanBeNull] string navigationName = null,
[CanBeNull] Action<NavigationBuilder> navigationConfiguration = null)
[CanBeNull] string navigationName = null)
{
Check.NotNull(relatedType, nameof(relatedType));
Check.NullButNotEmpty(navigationName, nameof(navigationName));

return HasMany(navigationName,
FindRelatedEntityType(relatedType, navigationName),
navigationConfiguration);
FindRelatedEntityType(relatedType, navigationName));
}

private CollectionNavigationBuilder HasMany(
string navigationName,
EntityType relatedEntityType,
Action<NavigationBuilder> navigationConfiguration = null)
EntityType relatedEntityType)
{
var skipNavigation = navigationName != null ? Builder.Metadata.FindSkipNavigation(navigationName) : null;

Expand All @@ -869,19 +826,11 @@ private CollectionNavigationBuilder HasMany(
.IsUnique(false, ConfigurationSource.Explicit);
}

var foreignKey = relationship?.Metadata;
if (navigationConfiguration != null
&& foreignKey?.PrincipalToDependent != null)
{
navigationConfiguration(
new NavigationBuilder(foreignKey.PrincipalToDependent));
}

return new CollectionNavigationBuilder(
Builder.Metadata,
relatedEntityType,
new MemberIdentity(navigationName),
foreignKey,
relationship?.Metadata,
skipNavigation);
}

Expand Down
Loading

0 comments on commit cd883db

Please sign in to comment.