Repository navigation
Conversation
…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
Contributor
There was a problem hiding this comment.
🟡 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
InvalidSetTypeWithNamefor 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)) |
Member
|
Marking the PR as draft until it's updated |
AndriySvyryd
marked this pull request as draft
November 13, 2026 05:29
Contributor
Author
|
I have updated the PR to address the review feedback:
All 327 tests in EFCore.Tests pass. PR is ready for review! |
SkyDevLab
marked this pull request as ready for review
November 26, 2026 18:48
…rRewritingConvention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #31565
Fixes #29970
Description
This PR improves exception messages when attempting to obtain a
DbSetusingcontext.Set<T>()orcontext.Set<T>(string entityTypeName)for shared-type entity types, join entities, or mismatched entity type names.1. Fix false "different namespace" message (
#31565甒)ModelExtensions.FindSameTypeNameWithDifferentNamespacepreviously checkedx.ClrType.DisplayName(false) == type.DisplayName(false)without verifyingx.ClrType != type. When an entity type with CLR typeTwas in the model but not found byFindEntityType(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:Added
x.ClrType != typeso 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,InternalDbSetpreviously checkedIsShared(typeof(TEntity))and threwCoreStrings.InvalidSetSharedType, instructing the user to use theSetoverload with an entity type name (which they had already used).Added
CoreStrings.InvalidSetTypeWithName:When
_entityTypeName != nulland not found in the model,InvalidSetTypeWithNameis now thrown.3. Detect shared CLR types without entity type name
In
InternalDbSet,DbContext.Finder, andQueryFilterRewritingConvention, added a check for whether any entity type in the model fortypeof(TEntity)has a shared CLR type (e.HasSharedClrType), throwingInvalidSetSharedTypewhenSet<T>()is called without an entity type name.4. Tests
DbContextTest.Set_throws_for_entity_type_name_not_in_modelinDbContextTest.cs.SharedTypeDbSetTest.Set_throws_for_shared_entity_type_name_not_in_modelandSet_throws_for_shared_entity_type_without_nameinSharedTypeDbSetTest.cs.