You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
EF Core provides a way to split and encapsulate entity configuration actions via IEntityTypeConfiguration<T> 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<T> 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.
The text was updated successfully, but these errors were encountered:
EF Core provides a way to split and encapsulate entity configuration actions via
IEntityTypeConfiguration<T>
interface. Moreover, it allows creating multiple implementations and register all of them fromOnModelCreating
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 theEntityTypeConfigurationAttribute
. 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 ofIEntityTypeConfiguration<T>
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.The text was updated successfully, but these errors were encountered: