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 for 15380. Generic KeyBuilder. #20516

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/EFCore.Relational/Extensions/RelationalKeyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public static KeyBuilder HasName([NotNull] this KeyBuilder keyBuilder, [CanBeNul
return keyBuilder;
}

/// <summary>
/// Configures the name of the key constraint in the database when targeting a relational database.
/// </summary>
/// <param name="keyBuilder"> The builder for the key being configured. </param>
/// <param name="name"> The name of the key. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static KeyBuilder<TEntity> HasName<TEntity>(
[NotNull] this KeyBuilder<TEntity> keyBuilder,
[CanBeNull] string name)
=> (KeyBuilder<TEntity>)HasName((KeyBuilder)keyBuilder, name);

/// <summary>
/// Configures the name of the key constraint in the database when targeting a relational database.
/// </summary>
Expand Down
30 changes: 26 additions & 4 deletions src/EFCore/Metadata/Builders/EntityTypeBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ public virtual EntityTypeBuilder<TEntity> HasBaseType<TBaseType>()
/// </para>
/// </param>
/// <returns> An object that can be used to configure the primary key. </returns>
public virtual KeyBuilder HasKey([NotNull] Expression<Func<TEntity, object>> keyExpression)
=> new KeyBuilder(
public virtual KeyBuilder<TEntity> HasKey([NotNull] Expression<Func<TEntity, object>> keyExpression)
=> new KeyBuilder<TEntity>(
Builder.PrimaryKey(
Check.NotNull(keyExpression, nameof(keyExpression)).GetPropertyAccessList(),
ConfigurationSource.Explicit).Metadata);

/// <summary>
/// Sets the properties that make up the primary key for this entity type.
/// </summary>
/// <param name="propertyNames"> The names of the properties that make up the primary key. </param>
/// <returns> An object that can be used to configure the primary key. </returns>
public new virtual KeyBuilder<TEntity> HasKey([NotNull] params string[] propertyNames)
=> new KeyBuilder<TEntity>(
Builder.PrimaryKey(
Check.NotEmpty(propertyNames, nameof(propertyNames)), ConfigurationSource.Explicit).Metadata);

/// <summary>
/// Creates an alternate key in the model for this entity type if one does not already exist over the specified
/// properties. This will force the properties to be read-only. Use <see cref="HasIndex" /> to specify uniqueness
Expand All @@ -106,12 +116,24 @@ public virtual KeyBuilder HasKey([NotNull] Expression<Func<TEntity, object>> key
/// </para>
/// </param>
/// <returns> An object that can be used to configure the key. </returns>
public virtual KeyBuilder HasAlternateKey([NotNull] Expression<Func<TEntity, object>> keyExpression)
=> new KeyBuilder(
public virtual KeyBuilder<TEntity> HasAlternateKey([NotNull] Expression<Func<TEntity, object>> keyExpression)
=> new KeyBuilder<TEntity>(
Builder.HasKey(
Check.NotNull(keyExpression, nameof(keyExpression)).GetPropertyAccessList(),
ConfigurationSource.Explicit).Metadata);

/// <summary>
/// Creates an alternate key in the model for this entity type if one does not already exist over the specified
/// properties. This will force the properties to be read-only. Use <see cref="HasIndex" /> to specify uniqueness
/// in the model that does not force properties to be read-only.
/// </summary>
/// <param name="propertyNames"> The names of the properties that make up the key. </param>
/// <returns> An object that can be used to configure the key. </returns>
public new virtual KeyBuilder<TEntity> HasAlternateKey([NotNull] params string[] propertyNames)
=> new KeyBuilder<TEntity>(
Builder.HasKey(
Check.NotEmpty(propertyNames, nameof(propertyNames)), ConfigurationSource.Explicit).Metadata);

/// <summary>
/// Configures the entity type to have no keys. It will only be usable for queries.
/// </summary>
Expand Down
43 changes: 43 additions & 0 deletions src/EFCore/Metadata/Builders/KeyBuilder`.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Microsoft.EntityFrameworkCore.Metadata.Builders
{
/// <summary>
/// <para>
/// Provides a simple API for configuring a <see cref="IMutableKey" />.
/// </para>
/// <para>
/// Instances of this class are returned from methods when using the <see cref="ModelBuilder" /> API
/// and it is not designed to be directly constructed in your application code.
/// </para>
/// </summary>
// ReSharper disable once UnusedTypeParameter
public class KeyBuilder<T> : KeyBuilder
{
/// <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]
public KeyBuilder([NotNull] IMutableKey key)
: base(key)
{
}

/// <summary>
/// Adds or updates an annotation on the key. If an annotation with the key specified in
/// <paramref name="annotation" /> already exists its value will be updated.
/// </summary>
/// <param name="annotation"> The key of the annotation to be added or updated. </param>
/// <param name="value"> The value to be stored in the annotation. </param>
/// <returns> The same builder instance so that multiple configuration calls can be chained. </returns>
public new virtual KeyBuilder<T> HasAnnotation([NotNull] string annotation, [NotNull] object value)
=> (KeyBuilder<T>)base.HasAnnotation(annotation, value);
}
}
14 changes: 12 additions & 2 deletions src/EFCore/Metadata/Builders/OwnedNavigationBuilder`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ public OwnedNavigationBuilder(
/// </para>
/// </param>
/// <returns> An object that can be used to configure the primary key. </returns>
public virtual KeyBuilder HasKey([NotNull] Expression<Func<TDependentEntity, object>> keyExpression)
=> new KeyBuilder(
public virtual KeyBuilder<TDependentEntity> HasKey([NotNull] Expression<Func<TDependentEntity, object>> keyExpression)
=> new KeyBuilder<TDependentEntity>(
DependentEntityType.Builder.PrimaryKey(
Check.NotNull(keyExpression, nameof(keyExpression)).GetPropertyAccessList(), ConfigurationSource.Explicit).Metadata);

/// <summary>
/// Sets the properties that make up the primary key for this owned entity type.
/// </summary>
/// <param name="propertyNames"> The names of the properties that make up the primary key. </param>
/// <returns> An object that can be used to configure the primary key. </returns>
public new virtual KeyBuilder<TDependentEntity> HasKey([NotNull] params string[] propertyNames)
=> new KeyBuilder<TDependentEntity>(
DependentEntityType.Builder.PrimaryKey(
Check.NotEmpty(propertyNames, nameof(propertyNames)), ConfigurationSource.Explicit).Metadata);

/// <summary>
/// <para>
/// Returns an object that can be used to configure a property of the owned entity type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,19 @@ public static ModelBuilderTest.TestIndexBuilder HasName(
return builder;
}

public static ModelBuilderTest.TestKeyBuilder HasName(
this ModelBuilderTest.TestKeyBuilder builder, string name)
public static ModelBuilderTest.TestKeyBuilder<TEntity> HasName<TEntity>(
this ModelBuilderTest.TestKeyBuilder<TEntity> builder, string name)
{
var keyBuilder = builder.GetInfrastructure();
keyBuilder.HasName(name);
switch (builder)
{
case IInfrastructure<KeyBuilder<TEntity>> genericBuilder:
genericBuilder.Instance.HasName(name);
break;
case IInfrastructure<KeyBuilder> nongenericBuilder:
nongenericBuilder.Instance.HasName(name);
break;
}

return builder;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/EFCore.Tests/Metadata/MetadataBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void Can_write_convention_key_builder_extension()
.KeyBuilderExtension("V1")
.KeyBuilderExtension("V2");

Assert.IsType<KeyBuilder>(returnedBuilder);
Assert.IsType<KeyBuilder<Gunter>>(returnedBuilder);

var model = builder.Model;
var key = model.FindEntityType(typeof(Gunter)).FindPrimaryKey();
Expand Down Expand Up @@ -298,7 +298,7 @@ public void Can_write_convention_key_builder_extension_with_common_name()
.SharedNameExtension("V1")
.SharedNameExtension("V2");

Assert.IsType<KeyBuilder>(returnedBuilder);
Assert.IsType<KeyBuilder<Gunter>>(returnedBuilder);

var model = builder.Model;
var key = model.FindEntityType(typeof(Gunter)).FindPrimaryKey();
Expand Down
41 changes: 29 additions & 12 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,17 @@ public override TestEntityTypeBuilder<TEntity> HasBaseType<TBaseEntity>()
public override TestEntityTypeBuilder<TEntity> HasBaseType(string baseEntityTypeName)
=> Wrap(EntityTypeBuilder.HasBaseType(baseEntityTypeName));

public override TestKeyBuilder HasKey(Expression<Func<TEntity, object>> keyExpression)
=> new TestKeyBuilder(EntityTypeBuilder.HasKey(keyExpression));
public override TestKeyBuilder<TEntity> HasKey(Expression<Func<TEntity, object>> keyExpression)
=> new GenericTestKeyBuilder<TEntity>(EntityTypeBuilder.HasKey(keyExpression));

public override TestKeyBuilder HasKey(params string[] propertyNames)
=> new TestKeyBuilder(EntityTypeBuilder.HasKey(propertyNames));
public override TestKeyBuilder<TEntity> HasKey(params string[] propertyNames)
=> new GenericTestKeyBuilder<TEntity>(EntityTypeBuilder.HasKey(propertyNames));

public override TestKeyBuilder HasAlternateKey(Expression<Func<TEntity, object>> keyExpression)
=> new TestKeyBuilder(EntityTypeBuilder.HasAlternateKey(keyExpression));
public override TestKeyBuilder<TEntity> HasAlternateKey(Expression<Func<TEntity, object>> keyExpression)
=> new GenericTestKeyBuilder<TEntity>(EntityTypeBuilder.HasAlternateKey(keyExpression));

public override TestKeyBuilder HasAlternateKey(params string[] propertyNames)
=> new TestKeyBuilder(EntityTypeBuilder.HasAlternateKey(propertyNames));
public override TestKeyBuilder<TEntity> HasAlternateKey(params string[] propertyNames)
=> new GenericTestKeyBuilder<TEntity>(EntityTypeBuilder.HasAlternateKey(propertyNames));

public override TestEntityTypeBuilder<TEntity> HasNoKey()
=> Wrap(EntityTypeBuilder.HasNoKey());
Expand Down Expand Up @@ -441,6 +441,23 @@ public override TestPropertyBuilder<TProperty> HasConversion(ValueConverter conv
PropertyBuilder<TProperty> IInfrastructure<PropertyBuilder<TProperty>>.Instance => PropertyBuilder;
}

protected class GenericTestKeyBuilder<TEntity> : TestKeyBuilder<TEntity>, IInfrastructure<KeyBuilder<TEntity>>
{
public GenericTestKeyBuilder(KeyBuilder<TEntity> keyBuilder)
{
KeyBuilder = keyBuilder;
}

private KeyBuilder<TEntity> KeyBuilder { get; }

public override IMutableKey Metadata => KeyBuilder.Metadata;

public override TestKeyBuilder<TEntity> HasAnnotation(string annotation, object value)
=> new GenericTestKeyBuilder<TEntity>(KeyBuilder.HasAnnotation(annotation, value));

KeyBuilder<TEntity> IInfrastructure<KeyBuilder<TEntity>>.Instance => KeyBuilder;
}

protected class GenericTestNavigationBuilder : TestNavigationBuilder
{
public GenericTestNavigationBuilder(NavigationBuilder navigationBuilder)
Expand Down Expand Up @@ -713,11 +730,11 @@ public override TestOwnedNavigationBuilder<TEntity, TDependentEntity> HasAnnotat
string annotation, object value)
=> Wrap(OwnedNavigationBuilder.HasAnnotation(annotation, value));

public override TestKeyBuilder HasKey(Expression<Func<TDependentEntity, object>> keyExpression)
=> new TestKeyBuilder(OwnedNavigationBuilder.HasKey(keyExpression));
public override TestKeyBuilder<TDependentEntity> HasKey(Expression<Func<TDependentEntity, object>> keyExpression)
=> new GenericTestKeyBuilder<TDependentEntity>(OwnedNavigationBuilder.HasKey(keyExpression));

public override TestKeyBuilder HasKey(params string[] propertyNames)
=> new TestKeyBuilder(OwnedNavigationBuilder.HasKey(propertyNames));
public override TestKeyBuilder<TDependentEntity> HasKey(params string[] propertyNames)
=> new GenericTestKeyBuilder<TDependentEntity>(OwnedNavigationBuilder.HasKey(propertyNames));

public override TestPropertyBuilder<TProperty> Property<TProperty>(string propertyName)
=> new GenericTestPropertyBuilder<TProperty>(OwnedNavigationBuilder.Property<TProperty>(propertyName));
Expand Down
41 changes: 29 additions & 12 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderNonGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ public override TestEntityTypeBuilder<TEntity> HasBaseType<TBaseEntity>()
public override TestEntityTypeBuilder<TEntity> HasBaseType(string baseEntityTypeName)
=> Wrap(EntityTypeBuilder.HasBaseType(baseEntityTypeName));

public override TestKeyBuilder HasKey(Expression<Func<TEntity, object>> keyExpression)
=> new TestKeyBuilder(
public override TestKeyBuilder<TEntity> HasKey(Expression<Func<TEntity, object>> keyExpression)
=> new NonGenericTestKeyBuilder<TEntity>(
EntityTypeBuilder.HasKey(keyExpression.GetPropertyAccessList().Select(p => p.GetSimpleMemberName()).ToArray()));

public override TestKeyBuilder HasKey(params string[] propertyNames)
=> new TestKeyBuilder(EntityTypeBuilder.HasKey(propertyNames));
public override TestKeyBuilder<TEntity> HasKey(params string[] propertyNames)
=> new NonGenericTestKeyBuilder<TEntity>(EntityTypeBuilder.HasKey(propertyNames));

public override TestKeyBuilder HasAlternateKey(Expression<Func<TEntity, object>> keyExpression)
=> new TestKeyBuilder(
public override TestKeyBuilder<TEntity> HasAlternateKey(Expression<Func<TEntity, object>> keyExpression)
=> new NonGenericTestKeyBuilder<TEntity>(
EntityTypeBuilder.HasAlternateKey(
keyExpression.GetPropertyAccessList().Select(p => p.GetSimpleMemberName()).ToArray()));

public override TestKeyBuilder HasAlternateKey(params string[] propertyNames)
=> new TestKeyBuilder(EntityTypeBuilder.HasAlternateKey(propertyNames));
public override TestKeyBuilder<TEntity> HasAlternateKey(params string[] propertyNames)
=> new NonGenericTestKeyBuilder<TEntity>(EntityTypeBuilder.HasAlternateKey(propertyNames));

public override TestEntityTypeBuilder<TEntity> HasNoKey()
=> Wrap(EntityTypeBuilder.HasNoKey());
Expand Down Expand Up @@ -429,6 +429,23 @@ public override TestNavigationBuilder UsePropertyAccessMode(PropertyAccessMode p
=> new NonGenericTestNavigationBuilder(NavigationBuilder.UsePropertyAccessMode(propertyAccessMode));
}

protected class NonGenericTestKeyBuilder<TEntity> : TestKeyBuilder<TEntity>, IInfrastructure<KeyBuilder>
{
public NonGenericTestKeyBuilder(KeyBuilder keyBuilder)
{
KeyBuilder = keyBuilder;
}

private KeyBuilder KeyBuilder { get; }

public override IMutableKey Metadata => KeyBuilder.Metadata;

public override TestKeyBuilder<TEntity> HasAnnotation(string annotation, object value)
=> new NonGenericTestKeyBuilder<TEntity>(KeyBuilder.HasAnnotation(annotation, value));

KeyBuilder IInfrastructure<KeyBuilder>.Instance => KeyBuilder;
}

protected class
NonGenericTestReferenceNavigationBuilder<TEntity, TRelatedEntity> : TestReferenceNavigationBuilder<TEntity, TRelatedEntity>
where TEntity : class
Expand Down Expand Up @@ -700,13 +717,13 @@ public override TestOwnedNavigationBuilder<TEntity, TDependentEntity> HasAnnotat
string annotation, object value)
=> Wrap<TEntity, TDependentEntity>(OwnedNavigationBuilder.HasAnnotation(annotation, value));

public override TestKeyBuilder HasKey(Expression<Func<TDependentEntity, object>> keyExpression)
=> new TestKeyBuilder(
public override TestKeyBuilder<TDependentEntity> HasKey(Expression<Func<TDependentEntity, object>> keyExpression)
=> new NonGenericTestKeyBuilder<TDependentEntity>(
OwnedNavigationBuilder.HasKey(
keyExpression.GetPropertyAccessList().Select(p => p.GetSimpleMemberName()).ToArray()));

public override TestKeyBuilder HasKey(params string[] propertyNames)
=> new TestKeyBuilder(OwnedNavigationBuilder.HasKey(propertyNames));
public override TestKeyBuilder<TDependentEntity> HasKey(params string[] propertyNames)
=> new NonGenericTestKeyBuilder<TDependentEntity>(OwnedNavigationBuilder.HasKey(propertyNames));

public override TestPropertyBuilder<TProperty> Property<TProperty>(string propertyName)
=> new NonGenericTestPropertyBuilder<TProperty>(OwnedNavigationBuilder.Property<TProperty>(propertyName));
Expand Down
Loading