Skip to content

Fix misleading exception messages when obtaining DbSet with entity type name (#31565) - #39342

Open
SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:fix/31565-shared-type-set-exception
Open

SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:fix/31565-shared-type-set-exception

Conversation

@SkyDevLab

Copy link
Copy Markdown
Contributor

Fixes #31565
Fixes #29970

Description

This PR improves exception messages when attempting to obtain a DbSet using context.Set<T>() or context.Set<T>(string entityTypeName) for shared-type entity types, join entities, or mismatched entity type names.

1. Fix false "different namespace" message (#31565 & #29970)

ModelExtensions.FindSameTypeNameWithDifferentNamespace previously checked x.ClrType.DisplayName(false) == type.DisplayName(false) without verifying x.ClrType != type. When an entity type with CLR type T was in the model but not found by FindEntityType (e.g. because it was registered with a specific entity type name or shared name), it matched against itself and returned the exact same type name. This resulted in the confusing message:

Cannot create a DbSet for 'MyType' because this type is not included in the model for the context. However the model contains an entity type with the same name in a different namespace: 'MyType'.

Added x.ClrType != type so that only genuinely different types across namespaces are matched.

2. Handle entity type name not found in model (#29970)

When context.Set<TEntity>(entityTypeName) is called with an entity type name that does not exist in the model, InternalDbSet previously checked IsShared(typeof(TEntity)) and threw CoreStrings.InvalidSetSharedType, instructing the user to use the Set overload with an entity type name (which they had already used).

Added CoreStrings.InvalidSetTypeWithName:

Cannot create a DbSet for '{typeName}' with entity type name '{entityTypeName}' because no entity type with this name is included in the model for the context.

When _entityTypeName != null and not found in the model, InvalidSetTypeWithName is now thrown.

3. Detect shared CLR types without entity type name

In InternalDbSet, DbContext.Finder, and QueryFilterRewritingConvention, added a check for whether any entity type in the model for typeof(TEntity) has a shared CLR type (e.HasSharedClrType), throwing InvalidSetSharedType when Set<T>() is called without an entity type name.

4. Tests

  • Added DbContextTest.Set_throws_for_entity_type_name_not_in_model in DbContextTest.cs.
  • Added SharedTypeDbSetTest.Set_throws_for_shared_entity_type_name_not_in_model and Set_throws_for_shared_entity_type_without_name in SharedTypeDbSetTest.cs.

…pe name (dotnet#31565)

- In ModelExtensions.FindSameTypeNameWithDifferentNamespace, ensure x.ClrType != type so an entity type is not falsely reported as being in a different namespace when its CLR type is identical.
- In InternalDbSet, throw InvalidSetTypeWithName when an entity type name is specified but not found in the model, rather than claiming the type is a shared type or in a different namespace.
- In DbContext.Finder and QueryFilterRewritingConvention, check if any entity type in the model for the CLR type has a shared CLR type before falling back to namespace matching.
- Add unit tests in DbContextTest and SharedTypeDbSetTest.

Closes dotnet#31565
Closes dotnet#29970

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.

🟡 Changes recommended

Query-filter rewriting still has two unresolved moderate diagnostic-handling issues.

1 open issue
1 file not reviewed
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file
What changed in this PR

This pull request improves DbSet diagnostics for shared-type entities, missing named entity types, and misleading same-name namespace suggestions.

Changes:

  • Adds InvalidSetTypeWithName for named entity types absent from the model.
  • Prevents self-matching in different-namespace diagnostics.
  • Extends shared CLR-type detection in InternalDbSet, DbContext, and query-filter rewriting.
  • Adds regression tests and updates the API baseline.

Two unresolved moderate issues remain in query-filter rewriting: member access can still produce a NullReferenceException for shared-only CLR types, and missing named entity types can still receive the misleading shared-type diagnostic. These findings received 1 and 4 votes, respectively.

File Description
test/​EFCore.Tests/​SharedTypeDbSetTest.cs Tests missing named shared entities and unnamed shared CLR types.
test/​EFCore.Tests/​DbContextTest.cs Tests the new missing entity type name diagnostic.
src/​EFCore/​Properties/​CoreStrings.resx Defines the new localized error message.
src/​EFCore/​Properties/​CoreStrings.Designer.cs Exposes the generated error string accessor.
src/​EFCore/​Metadata/​Internal/​ModelExtensions.cs Excludes the requested CLR type from namespace-difference matching.
src/​EFCore/​Metadata/​Conventions/​QueryFilterRewritingConvention.cs Adds shared CLR-type detection during query-filter rewriting; unresolved issues remain for member access and missing named types.
src/​EFCore/​Internal/​InternalDbSet.cs Reports the new name-specific error and detects shared CLR types.
src/​EFCore/​EFCore.baseline.json Records the new CoreStrings API member.
src/​EFCore/​DbContext.cs Improves shared CLR-type detection for entity finders.

Review effort: Lite

Comment on lines +140 to +141
if (_model.IsShared(entityClrType)
|| ((IModel)_model).FindEntityTypes(entityClrType).Any(e => e.HasSharedClrType))
@AndriySvyryd

Copy link
Copy Markdown
Member

Marking the PR as draft until it's updated

@AndriySvyryd
AndriySvyryd marked this pull request as draft November 13, 2026 05:29
@SkyDevLab

Copy link
Copy Markdown
Contributor Author

I have updated the PR to address the review feedback:

  • QueryFilterRewritingConvention now preserves InvalidSetTypeWithName when a named entity type is requested (Set(missing)) before falling back to shared-type / standard InvalidSetType checks.
  • Handled member access (context.Posts) when entityType == null to report the proper diagnostic (InvalidSetSharedType, etc.) rather than passing null into EntityQueryRootExpression.
  • Added convention regression tests in QueryFilterRewritingConventionTest:
    • QueryFilter_containing_db_set_with_not_included_named_type
    • QueryFilter_containing_db_set_member_access_with_shared_type

All 327 tests in EFCore.Tests pass. PR is ready for review!

@SkyDevLab
SkyDevLab marked this pull request as ready for review November 26, 2026 18:48
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.

Improve exception messages for shared-type entity types Confusing exception messages when failing to obtain DbSet with shared-type entity types

3 participants