Skip to content

Commit

Permalink
Updated per review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
lajones committed Jul 2, 2020
1 parent a744b58 commit 31d8567
Show file tree
Hide file tree
Showing 19 changed files with 404 additions and 256 deletions.
9 changes: 8 additions & 1 deletion src/EFCore/Metadata/Builders/CollectionCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.ComponentModel;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -94,13 +95,19 @@ public virtual EntityTypeBuilder UsingEntity(
[NotNull] Func<EntityTypeBuilder, ReferenceCollectionBuilder> configureRight,
[NotNull] Func<EntityTypeBuilder, ReferenceCollectionBuilder> configureLeft)
{
if (((Model)LeftEntityType.Model).IsShared(joinEntity))
{
throw new InvalidOperationException(
CoreStrings.DoNotUseUsingEntityOnSharedClrType(joinEntity.GetType().Name));
}

var existingAssociationEntityType = (EntityType)
(LeftNavigation.ForeignKey?.DeclaringEntityType
?? RightNavigation.ForeignKey?.DeclaringEntityType);
if (existingAssociationEntityType != null)
{
ModelBuilder.RemoveAssociationEntityIfAutomaticallyCreated(
existingAssociationEntityType, false, ConfigurationSource.Explicit);
existingAssociationEntityType, removeSkipNavigations: false, ConfigurationSource.Explicit);
}

var entityTypeBuilder = new EntityTypeBuilder(
Expand Down
9 changes: 8 additions & 1 deletion src/EFCore/Metadata/Builders/CollectionCollectionBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

Expand Down Expand Up @@ -51,13 +52,19 @@ public virtual EntityTypeBuilder<TAssociationEntity> UsingEntity<TAssociationEnt
[NotNull] Func<EntityTypeBuilder<TAssociationEntity>, ReferenceCollectionBuilder<TRightEntity, TAssociationEntity>> configureLeft)
where TAssociationEntity : class
{
if (((Model)LeftEntityType.Model).IsShared(typeof(TAssociationEntity)))
{
throw new InvalidOperationException(
CoreStrings.DoNotUseUsingEntityOnSharedClrType(typeof(TAssociationEntity).Name));
}

var existingAssociationEntityType = (EntityType)
(LeftNavigation.ForeignKey?.DeclaringEntityType
?? RightNavigation.ForeignKey?.DeclaringEntityType);
if (existingAssociationEntityType != null)
{
ModelBuilder.RemoveAssociationEntityIfAutomaticallyCreated(
existingAssociationEntityType, false, ConfigurationSource.Explicit);
existingAssociationEntityType, removeSkipNavigations: false, ConfigurationSource.Explicit);
}

var entityTypeBuilder = new EntityTypeBuilder<TAssociationEntity>(
Expand Down
16 changes: 11 additions & 5 deletions src/EFCore/Metadata/Builders/CollectionNavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,17 @@ public virtual CollectionCollectionBuilder WithMany([NotNull] string navigationN
}

var leftName = Builder?.Metadata.PrincipalToDependent.Name;
return new CollectionCollectionBuilder(
RelatedEntityType,
DeclaringEntityType,
WithLeftManyNavigation(navigationName),
WithRightManyNavigation(navigationName, leftName));
var collectionCollectionBuilder =
new CollectionCollectionBuilder(
RelatedEntityType,
DeclaringEntityType,
WithLeftManyNavigation(navigationName),
WithRightManyNavigation(navigationName, leftName));

collectionCollectionBuilder.LeftNavigation
.SetInverse(collectionCollectionBuilder.RightNavigation);

return collectionCollectionBuilder;
}

/// <summary>
Expand Down
32 changes: 22 additions & 10 deletions src/EFCore/Metadata/Builders/CollectionNavigationBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,17 @@ public virtual ReferenceCollectionBuilder<TEntity, TRelatedEntity> WithOne(
public new virtual CollectionCollectionBuilder<TRelatedEntity, TEntity> WithMany([NotNull] string navigationName)
{
var leftName = Builder?.Metadata.PrincipalToDependent.Name;
return new CollectionCollectionBuilder<TRelatedEntity, TEntity>(
RelatedEntityType,
DeclaringEntityType,
WithLeftManyNavigation(navigationName),
WithRightManyNavigation(navigationName, leftName));
var collectionCollectionBuilder =
new CollectionCollectionBuilder<TRelatedEntity, TEntity>(
RelatedEntityType,
DeclaringEntityType,
WithLeftManyNavigation(navigationName),
WithRightManyNavigation(navigationName, leftName));

collectionCollectionBuilder.LeftNavigation
.SetInverse(collectionCollectionBuilder.RightNavigation);

return collectionCollectionBuilder;
}

/// <summary>
Expand All @@ -126,11 +132,17 @@ public virtual CollectionCollectionBuilder<TRelatedEntity, TEntity> WithMany(
}

var leftName = Builder?.Metadata.PrincipalToDependent.Name;
return new CollectionCollectionBuilder<TRelatedEntity, TEntity>(
RelatedEntityType,
DeclaringEntityType,
WithLeftManyNavigation(navigationExpression.GetMemberAccess()),
WithRightManyNavigation(navigationExpression.GetMemberAccess(), leftName));
var collectionCollectionBuilder =
new CollectionCollectionBuilder<TRelatedEntity, TEntity>(
RelatedEntityType,
DeclaringEntityType,
WithLeftManyNavigation(navigationExpression.GetMemberAccess()),
WithRightManyNavigation(navigationExpression.GetMemberAccess(), leftName));

collectionCollectionBuilder.LeftNavigation
.SetInverse(collectionCollectionBuilder.RightNavigation);

return collectionCollectionBuilder;
}
}
}
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Conventions/BackingFieldConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private FieldInfo GetFieldToSet(IConventionPropertyBase propertyBase)
if (propertyBase == null
|| !ConfigurationSource.Convention.Overrides(propertyBase.GetFieldInfoConfigurationSource())
|| propertyBase.IsIndexerProperty()
|| (propertyBase.PropertyInfo == null && propertyBase.FieldInfo == null))
|| propertyBase.IsShadowProperty())
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public virtual ConventionSet CreateConventionSet()

conventionSet.NavigationRemovedConventions.Add(relationshipDiscoveryConvention);

conventionSet.SkipNavigationAddedConventions.Add(new ManyToManyConvention(Dependencies));
conventionSet.SkipNavigationAddedConventions.Add(new ManyToManyAssociationEntityTypeConvention(Dependencies));

conventionSet.IndexAddedConventions.Add(foreignKeyIndexConvention);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
/// <summary>
/// A convention which looks for matching skip navigations and automatically creates
/// a many-to-many association entity with suitable foreign keys, sets the two
/// matching skip navigations to use those foreign keys and makes them inverses of
/// one another.
/// matching skip navigations to use those foreign keys.
/// </summary>
public class ManyToManyConvention : ISkipNavigationAddedConvention
public class ManyToManyAssociationEntityTypeConvention : ISkipNavigationAddedConvention
{
/// <summary>
/// Creates a new instance of <see cref="ManyToManyConvention" />.
/// Creates a new instance of <see cref="ManyToManyAssociationEntityTypeConvention" />.
/// </summary>
/// <param name="dependencies"> Parameter object containing dependencies for this convention. </param>
public ManyToManyConvention([NotNull] ProviderConventionSetBuilderDependencies dependencies)
public ManyToManyAssociationEntityTypeConvention([NotNull] ProviderConventionSetBuilderDependencies dependencies)
{
Dependencies = dependencies;
}
Expand All @@ -41,8 +40,8 @@ public virtual void ProcessSkipNavigationAdded(
IConventionSkipNavigationBuilder skipNavigationBuilder,
IConventionContext<IConventionSkipNavigationBuilder> context)
{
Check.NotNull(skipNavigationBuilder, "skipNavigationBuilder");
Check.NotNull(context, "context");
Check.NotNull(skipNavigationBuilder, nameof(skipNavigationBuilder));
Check.NotNull(context, nameof(context));

var skipNavigation = skipNavigationBuilder.Metadata;
if (skipNavigation.ForeignKey != null
Expand All @@ -55,13 +54,10 @@ public virtual void ProcessSkipNavigationAdded(
return;
}

var matchingSkipNavigation = skipNavigation.TargetEntityType
.GetSkipNavigations()
.FirstOrDefault(sn => sn.TargetEntityType == skipNavigation.DeclaringEntityType);

if (matchingSkipNavigation == null
|| matchingSkipNavigation.ForeignKey != null
|| !matchingSkipNavigation.IsCollection)
var inverseSkipNavigation = skipNavigation.Inverse;
if (inverseSkipNavigation == null
|| inverseSkipNavigation.ForeignKey != null
|| !inverseSkipNavigation.IsCollection)
{
// do not create an automatic many-to-many association entity type if
// the matching skip navigation is already "in use" (i.e.
Expand All @@ -70,9 +66,9 @@ public virtual void ProcessSkipNavigationAdded(
}

var model = (Model)skipNavigation.DeclaringEntityType.Model;
model.Builder.AssociationEntity(
model.Builder.HasAutomaticAssociationEntity(
(SkipNavigation)skipNavigation,
(SkipNavigation)matchingSkipNavigation,
(SkipNavigation)inverseSkipNavigation,
ConfigurationSource.Convention);
}
}
Expand Down
Loading

0 comments on commit 31d8567

Please sign in to comment.