dotnet / dotnet/efcore

Allow multiple EntityTypeConfigurationAttribute for entity types

Open
#34,710 1 comment 0 reactions 0 assignees View on GitHub
area-model-building customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

EF Core provides a way to split and encapsulate entity configuration actions via `IEntityTypeConfiguration` interface. Moreover, it allows creating multiple implementations and register all of them from `OnModelCreating` method.

However, in some cases it is more convenient to keep `OnModelCreating` method untouched (e.g. when you deal with source generation and have no access to the database context class, but only to the entity type). For that purpose EF Core provides the `EntityTypeConfigurationAttribute`. It marks an entity class slecifying the type of the mentioned above interface realization, that should be used for the entity configuring.

The main problem is that this attribute doesn't allow multiple declarations on the same type. So, you cannot specify several configuration classes for the entity type.

**Scenario where such a feature is convenient**
You have two source generators. Both of them generate some logic for configuration entities and have access to the entities assemblies. But none of them has access to an assembly where the database context is declared. Keep in mind, that generators are independent and cannot be synchronized. In that case you could make entities be partial and each generator could create additional partial declaration of the entity whith `EntityTypeConfigurationAttribute` pointing to the corresponding generated implementation of `IEntityTypeConfiguration` interface.

The `EntityTypeConfigurationAttribute` should allow multiple declarations on the same entity type. EF Core should analyze all of them, ignore duplicates in specified configuration classes types, and then apply configurations in any order. Alternatively, the attribute can provide an integer order property to specify aplying order.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.