Skip to content

Commit

Permalink
Fix #30072 : Add Generic version of EntityTypeConfiguration Attribute (
Browse files Browse the repository at this point in the history
  • Loading branch information
OsamaAbuSitta authored Apr 25, 2023
1 parent 07284ac commit bcc870a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
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

0 comments on commit bcc870a

Please sign in to comment.