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 #30072 : Add Generic version of EntityTypeConfiguration Attribute #30653

Merged
merged 4 commits into from
Apr 25, 2023
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
5 changes: 4 additions & 1 deletion src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.EntityFrameworkCore;
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
[AttributeUsage(AttributeTargets.Class)]
public sealed class EntityTypeConfigurationAttribute : Attribute
public class EntityTypeConfigurationAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="EntityTypeConfigurationAttribute" /> class.
Expand All @@ -32,3 +32,6 @@ public EntityTypeConfigurationAttribute(Type entityConfigurationType)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.Interfaces)]
public Type EntityTypeConfigurationType { get; }
}



24 changes: 24 additions & 0 deletions src/EFCore/EntityTypeConfigurationAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Specifies the configuration type for the entity type.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <typeparam name="TConfiguration">The IEntityTypeConfiguration&lt;&gt; type to use.</typeparam>
/// <typeparam name="TEntity">The entity type to be configured.</typeparam>
[AttributeUsage(AttributeTargets.Class)]
public sealed class EntityTypeConfigurationAttribute<TConfiguration, TEntity> : EntityTypeConfigurationAttribute
where TConfiguration : class , IEntityTypeConfiguration<TEntity>
where TEntity : class
{
/// <summary>
/// Initializes a new instance of the <see cref="EntityTypeConfigurationAttribute" /> class.
/// </summary>
public EntityTypeConfigurationAttribute() : base(typeof(TConfiguration)) { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public void EntityTypeConfigurationAttribute_should_apply_configuration_to_Entit
Assert.Equal(1000, entityType.FindProperty(nameof(Customer.Name)).GetMaxLength());
}

[ConditionalFact]
public void EntityTypeConfigurationAttribute_should_apply_configuration_to_EntityType_Generic()
{
var builder = InMemoryTestHelpers.Instance.CreateConventionBuilder();

builder.Entity<CustomerGeneric>();

var entityType = builder.Model.FindEntityType(typeof(CustomerGeneric));
Assert.Equal(1000, entityType.FindProperty(nameof(CustomerGeneric.Name)).GetMaxLength());
}

[ConditionalFact]
public void EntityTypeConfigurationAttribute_should_throw_when_configuration_is_wrong_type()
{
Expand Down Expand Up @@ -59,6 +70,12 @@ public void Configure(EntityTypeBuilder<Customer> builder)
=> builder.Property(c => c.Name).HasMaxLength(1000);
}

private class CustomerGenericConfiguration : IEntityTypeConfiguration<CustomerGeneric>
{
public void Configure(EntityTypeBuilder<CustomerGeneric> builder)
=> builder.Property(c => c.Name).HasMaxLength(1000);
}

[EntityTypeConfiguration(typeof(CustomerConfiguration))]
private class Customer
{
Expand All @@ -67,6 +84,15 @@ private class Customer
public string Name { get; set; }
}


[EntityTypeConfigurationAttribute<CustomerGenericConfiguration, CustomerGeneric>]
private class CustomerGeneric
{
public int Id { get; set; }

public string Name { get; set; }
}

[EntityTypeConfiguration(typeof(CustomerConfiguration))]
private class InvalidCustomer
{
Expand Down