Skip to content

Allow multiple EntityTypeConfigurationAttribute declarations on entity types - #39506

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
Manish-1kumar:feature/issue-34710-multiple-entity-configuration-attributes
Nov 17, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
Manish-1kumar:feature/issue-34710-multiple-entity-configuration-attributes

Conversation

@Manish-1kumar

Copy link
Copy Markdown
Contributor
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Description

Fixes #34710

Enables multiple [EntityTypeConfiguration] declarations on a single entity type, allowing independent source generators and modular configuration classes across partial class definitions.

Key Architectural Decisions

  1. AllowMultiple = true, Inherited = false:
    Both EntityTypeConfigurationAttribute (EFCore.Abstractions) and EntityTypeConfigurationAttribute<TConfiguration, TEntity> (EFCore) are updated with AllowMultiple = true, Inherited = false.
    Inherited = false is critical: because IEntityTypeConfiguration<T> is strictly bound to T, inheriting base-class attributes on derived types would fail the interface type validation with InvalidOperationException. Setting Inherited = false ensures base classes configure base types and derived classes configure derived types, while preserving model-level facet inheritance.

  2. Deduplication:
    EntityTypeConfigurationAttributeConvention overrides ProcessEntityTypeAdded to deduplicate by attribute.EntityTypeConfigurationType per entity type using a local HashSet<Type>, ensuring identical configurations specified across partial classes only invoke Configure() once.

  3. Ordering:
    Per C# Language Specification §14.2.1, attribute ordering across partial types is explicitly unspecified. Multiple configurations execute sequentially in the order discovered with ConfigurationSource.Explicit semantics, with formal topological ordering remaining aligned with Allow to specify dependencies in IEntityTypeConfiguration #22035.

  4. Zero Impact on Other Conventions:
    TypeAttributeConventionBase is left untouched, isolating this change exclusively to EntityTypeConfigurationAttributeConvention.

Customer Impact

Developers and source generator authors can now annotate multiple partial declarations of an entity with independent [EntityTypeConfiguration] attributes without encountering CS0579: Duplicate 'EntityTypeConfiguration' attribute.

Testing

  • Added unit tests for multiple non-generic attributes, generic attributes, mixed attributes, partial class declarations, duplicate deduplication, and base/derived inheritance hierarchies in EntityTypeConfigurationAttributeConventionTest.
  • Verified all 624 convention tests in Microsoft.EntityFrameworkCore.Metadata.Conventions and ApiConsistencyTest pass locally.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain, and the changes include focused implementation updates with tests covering multiple, duplicate, invalid, partial, and inheritance scenarios.

No open issues 🎉

What changed in this PR

This pull request enables multiple EntityTypeConfigurationAttribute declarations on an entity, supporting modular and source-generated configuration across partial classes while preventing inherited configuration attributes from applying to derived types.

Changes:

  • Allows repeated non-generic and generic attribute declarations with AllowMultiple = true and Inherited = false.
  • Processes all direct configuration attributes and deduplicates configuration types per entity.
  • Adds tests for multiple, mixed, partial, duplicate, invalid, and inheritance scenarios.
  • Includes a minor test-coverage note to verify inherited model facets remain visible on derived types.
File Summary
test/​EFCore.Tests/​Metadata/​Conventions/​EntityTypeConfigurationAttributeConventionTest.cs Adds coverage for multiple configurations, partial classes, deduplication, mixed attributes, invalid configurations, and inheritance behavior.
src/​EFCore/​Metadata/​Conventions/​EntityTypeConfigurationAttributeConvention.cs Processes direct configuration attributes and applies each distinct configuration type once.
src/​EFCore/​EntityTypeConfigurationAttribute.cs Allows multiple non-inherited generic configuration attribute declarations.
src/​EFCore.Abstractions/​EntityTypeConfigurationAttribute.cs Allows multiple non-inherited non-generic configuration attribute declarations.

Review effort: Lite

@Manish-1kumar
Manish-1kumar force-pushed the feature/issue-34710-multiple-entity-configuration-attributes branch from ab6fd0a to 4a0d801 Compare November 4, 2026 08:58
@AndriySvyryd AndriySvyryd self-assigned this Nov 7, 2026

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit b4a52f2 into dotnet:main Nov 17, 2026
16 of 19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 18, 2026
@Manish-1kumar
Manish-1kumar deleted the feature/issue-34710-multiple-entity-configuration-attributes branch December 2, 2026 03:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow multiple EntityTypeConfigurationAttribute for entity types

3 participants