Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an OwnsOne overload not passing through the type. #22050

Merged
1 commit merged into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/EFCore/Metadata/Builders/EntityTypeBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,17 @@ public virtual OwnedNavigationBuilder<TEntity, TRelatedEntity> OwnsOne<TRelatedE
/// </para>
/// </summary>
/// <typeparam name="TRelatedEntity"> The entity type that this relationship targets. </typeparam>
/// <param name="entityTypeName"> The name of the target entity. </param>
/// <param name="ownedTypeName"> The name of the entity type that this relationship targets. </param>
/// <param name="navigationName">
/// The name of the reference navigation property on this entity type that represents the relationship.
/// </param>
/// <returns> An object that can be used to configure the owned type and the relationship. </returns>
public virtual OwnedNavigationBuilder<TEntity, TRelatedEntity> OwnsOne<TRelatedEntity>(
[NotNull] string entityTypeName,
[NotNull] string ownedTypeName,
[NotNull] string navigationName)
where TRelatedEntity : class
=> OwnsOneBuilder<TRelatedEntity>(
new TypeIdentity(Check.NotEmpty(entityTypeName, nameof(entityTypeName)), typeof(TRelatedEntity)),
new TypeIdentity(Check.NotEmpty(ownedTypeName, nameof(ownedTypeName)), typeof(TRelatedEntity)),
new MemberIdentity(Check.NotEmpty(navigationName, nameof(navigationName))));

/// <summary>
Expand Down Expand Up @@ -431,18 +431,18 @@ public virtual OwnedNavigationBuilder<TEntity, TRelatedEntity> OwnsOne<TRelatedE
/// </para>
/// </summary>
/// <typeparam name="TRelatedEntity"> The entity type that this relationship targets. </typeparam>
/// <param name="entityTypeName"> The name of the target entity. </param>
/// <param name="ownedTypeName"> The name of the entity type that this relationship targets. </param>
/// <param name="navigationExpression">
/// A lambda expression representing the reference navigation property on this entity type that represents
/// the relationship (<c>customer => customer.Address</c>).
/// </param>
/// <returns> An object that can be used to configure the owned type and the relationship. </returns>
public virtual OwnedNavigationBuilder<TEntity, TRelatedEntity> OwnsOne<TRelatedEntity>(
[NotNull] string entityTypeName,
[NotNull] string ownedTypeName,
[NotNull] Expression<Func<TEntity, TRelatedEntity>> navigationExpression)
where TRelatedEntity : class
=> OwnsOneBuilder<TRelatedEntity>(
new TypeIdentity(Check.NotEmpty(entityTypeName, nameof(entityTypeName)), typeof(TRelatedEntity)),
new TypeIdentity(Check.NotEmpty(ownedTypeName, nameof(ownedTypeName)), typeof(TRelatedEntity)),
new MemberIdentity(Check.NotNull(navigationExpression, nameof(navigationExpression)).GetMemberAccess()));

/// <summary>
Expand Down Expand Up @@ -682,7 +682,7 @@ public virtual EntityTypeBuilder<TEntity> OwnsOne<TRelatedEntity>(
Check.NotNull(buildAction, nameof(buildAction));

buildAction(OwnsOneBuilder<TRelatedEntity>(
new TypeIdentity(ownedTypeName), new MemberIdentity(navigationExpression.GetMemberAccess())));
new TypeIdentity(ownedTypeName, typeof(TRelatedEntity)), new MemberIdentity(navigationExpression.GetMemberAccess())));
return this;
}

Expand Down Expand Up @@ -1045,25 +1045,25 @@ public virtual EntityTypeBuilder<TEntity> OwnsMany<TRelatedEntity>(
/// </para>
/// </summary>
/// <typeparam name="TRelatedEntity"> The entity type that this relationship targets. </typeparam>
/// <param name="entityTypeName"> The name of the target entity. </param>
/// <param name="ownedTypeName"> The name of the entity type that this relationship targets. </param>
/// <param name="navigationExpression">
/// A lambda expression representing the reference navigation property on this entity type that represents
/// the relationship (<c>customer => customer.Address</c>).
/// </param>
/// <param name="buildAction"> An action that performs configuration of the owned type and the relationship. </param>
/// <returns> An object that can be used to configure the entity type. </returns>
public virtual EntityTypeBuilder<TEntity> OwnsMany<TRelatedEntity>(
[NotNull] string entityTypeName,
[NotNull] string ownedTypeName,
[NotNull] Expression<Func<TEntity, IEnumerable<TRelatedEntity>>> navigationExpression,
[NotNull] Action<OwnedNavigationBuilder<TEntity, TRelatedEntity>> buildAction)
where TRelatedEntity : class
{
Check.NotEmpty(entityTypeName, nameof(entityTypeName));
Check.NotEmpty(ownedTypeName, nameof(ownedTypeName));
Check.NotNull(navigationExpression, nameof(navigationExpression));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(OwnsManyBuilder<TRelatedEntity>(
new TypeIdentity(entityTypeName, typeof(TRelatedEntity)), new MemberIdentity(navigationExpression.GetMemberAccess())));
new TypeIdentity(ownedTypeName, typeof(TRelatedEntity)), new MemberIdentity(navigationExpression.GetMemberAccess())));
return this;
}

Expand Down
4 changes: 4 additions & 0 deletions src/EFCore/Metadata/Internal/InternalForeignKeyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ private bool CanSetNavigations(
}

if (navigationToPrincipalProperty != null
&& dependentEntityType.ClrType != null
&& principalEntityType.ClrType != null
&& !IsCompatible(
navigationToPrincipalProperty,
pointsToPrincipal: true,
Expand All @@ -763,6 +765,8 @@ private bool CanSetNavigations(
}

if (navigationToDependentProperty != null
&& dependentEntityType.ClrType != null
&& principalEntityType.ClrType != null
&& !IsCompatible(
navigationToDependentProperty,
pointsToPrincipal: false,
Expand Down
33 changes: 32 additions & 1 deletion src/EFCore/Metadata/Internal/TypeIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using JetBrains.Annotations;

Expand All @@ -14,7 +15,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public readonly struct TypeIdentity
public readonly struct TypeIdentity : IEquatable<TypeIdentity>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -83,5 +84,35 @@ public TypeIdentity([NotNull] Type type, [NotNull] Model model)
public bool IsNamed { [DebuggerStepThrough] get; }

private string DebuggerDisplay() => Name;

/// <inheritdoc />
public override bool Equals(object obj) => obj is TypeIdentity identity && Equals(identity);

/// <inheritdoc />
public bool Equals(TypeIdentity other)
=> Name == other.Name
&& EqualityComparer<Type>.Default.Equals(Type, other.Type)
&& IsNamed == other.IsNamed;

/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(Name, Type, IsNamed);

/// <summary>
/// Compares one id to another id to see if they represent the same type.
/// </summary>
/// <param name="left"> The first id. </param>
/// <param name="right"> The second id. </param>
/// <returns> <see langword="true"/> if they represent the same type; <see langword="false"/> otherwise. </returns>
public static bool operator ==(TypeIdentity left, TypeIdentity right)
=> left.Equals(right);

/// <summary>
/// Compares one id to another id to see if they represent different types.
/// </summary>
/// <param name="left"> The first id. </param>
/// <param name="right"> The second id. </param>
/// <returns> <see langword="true"/> if they represent different types; <see langword="false"/> otherwise. </returns>
public static bool operator !=(TypeIdentity left, TypeIdentity right)
=> !(left == right);
}
}
32 changes: 31 additions & 1 deletion src/EFCore/Metadata/MemberIdentity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using JetBrains.Annotations;
Expand All @@ -11,7 +13,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata
/// Represents the identity of an entity type member, can be based on <see cref="MemberInfo" /> or just the name.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public readonly struct MemberIdentity
public readonly struct MemberIdentity : IEquatable<MemberIdentity>
{
private readonly object _nameOrMember;

Expand Down Expand Up @@ -88,5 +90,33 @@ public MemberInfo MemberInfo

private string DebuggerDisplay()
=> Name ?? "NONE";

/// <inheritdoc />
public override bool Equals(object obj) => obj is MemberIdentity identity && Equals(identity);

/// <inheritdoc />
public bool Equals(MemberIdentity other)
=> EqualityComparer<object>.Default.Equals(_nameOrMember, other._nameOrMember);

/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_nameOrMember);

/// <summary>
/// Compares one id to another id to see if they represent the same member.
/// </summary>
/// <param name="left"> The first id. </param>
/// <param name="right"> The second id. </param>
/// <returns> <see langword="true"/> if they represent the same member; <see langword="false"/> otherwise. </returns>
public static bool operator ==(MemberIdentity left, MemberIdentity right)
=> left.Equals(right);

/// <summary>
/// Compares one id to another id to see if they represent different members.
/// </summary>
/// <param name="left"> The first id. </param>
/// <param name="right"> The second id. </param>
/// <returns> <see langword="true"/> if they represent different members; <see langword="false"/> otherwise. </returns>
public static bool operator !=(MemberIdentity left, MemberIdentity right)
=> !(left == right);
}
}
10 changes: 8 additions & 2 deletions test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,12 +1521,18 @@ public virtual void Shared_type_can_be_used_as_owned_type()
modelBuilder.Entity<OwnerOfSharedType>(
b =>
{
b.OwnsOne<Dictionary<string, object>>("Shared1", e => e.Reference).IndexerProperty<int>("Value");
b.OwnsOne<Dictionary<string, object>>("Shared1", e => e.Reference, sb =>
{
sb.IndexerProperty<int>("Value");
});
b.OwnsMany("Shared2", e => e.Collection).IndexerProperty<bool>("IsDeleted");
b.OwnsOne(e => e.OwnedNavigation,
o =>
{
o.OwnsOne<Dictionary<string, object>>("Shared3", e => e.Reference).IndexerProperty<int>("NestedValue");
o.OwnsOne<Dictionary<string, object>>("Shared3", e => e.Reference, sb =>
{
sb.IndexerProperty<int>("NestedValue");
});
o.OwnsMany("Shared4", e => e.Collection).IndexerProperty<long>("NestedLong");
});
});
Expand Down