diff --git a/src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs b/src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs index adbfbc7f0e3..3b1485e5737 100644 --- a/src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs +++ b/src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs @@ -13,7 +13,7 @@ namespace Microsoft.EntityFrameworkCore; /// See Modeling entity types and relationships for more information and examples. /// [AttributeUsage(AttributeTargets.Class)] -public sealed class EntityTypeConfigurationAttribute : Attribute +public class EntityTypeConfigurationAttribute : Attribute { /// /// Initializes a new instance of the class. @@ -32,3 +32,6 @@ public EntityTypeConfigurationAttribute(Type entityConfigurationType) [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.Interfaces)] public Type EntityTypeConfigurationType { get; } } + + + diff --git a/src/EFCore/EntityTypeConfigurationAttribute.cs b/src/EFCore/EntityTypeConfigurationAttribute.cs new file mode 100644 index 00000000000..4420042fedc --- /dev/null +++ b/src/EFCore/EntityTypeConfigurationAttribute.cs @@ -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; + +/// +/// Specifies the configuration type for the entity type. +/// +/// +/// See Modeling entity types and relationships for more information and examples. +/// +/// The IEntityTypeConfiguration<> type to use. +/// The entity type to be configured. +[AttributeUsage(AttributeTargets.Class)] +public sealed class EntityTypeConfigurationAttribute : EntityTypeConfigurationAttribute +where TConfiguration : class , IEntityTypeConfiguration +where TEntity : class +{ + /// + /// Initializes a new instance of the class. + /// + public EntityTypeConfigurationAttribute() : base(typeof(TConfiguration)) { } + +} diff --git a/test/EFCore.Tests/Metadata/Conventions/EntityTypeConfigurationAttributeConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/EntityTypeConfigurationAttributeConventionTest.cs index 70a2df6d647..bbce08daa65 100644 --- a/test/EFCore.Tests/Metadata/Conventions/EntityTypeConfigurationAttributeConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/EntityTypeConfigurationAttributeConventionTest.cs @@ -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(); + + 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() { @@ -59,6 +70,12 @@ public void Configure(EntityTypeBuilder builder) => builder.Property(c => c.Name).HasMaxLength(1000); } + private class CustomerGenericConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + => builder.Property(c => c.Name).HasMaxLength(1000); + } + [EntityTypeConfiguration(typeof(CustomerConfiguration))] private class Customer { @@ -67,6 +84,15 @@ private class Customer public string Name { get; set; } } + + [EntityTypeConfigurationAttribute] + private class CustomerGeneric + { + public int Id { get; set; } + + public string Name { get; set; } + } + [EntityTypeConfiguration(typeof(CustomerConfiguration))] private class InvalidCustomer {