Support file-scoped types as entity types#38215
Open
m-x-shokhzod wants to merge 2 commits intodotnet:mainfrom
Open
Support file-scoped types as entity types#38215m-x-shokhzod wants to merge 2 commits intodotnet:mainfrom
m-x-shokhzod wants to merge 2 commits intodotnet:mainfrom
Conversation
For `file class` / `file record` declarations, the C# compiler synthesizes a metadata name of the form `<FileName>F<hex>__UserTypeName`. The existing implementation of `IReadOnlyTypeBase.ShortName` looks for the first `<` in the type's short display name to strip generic type arguments. For file-scoped types that `<` is at index 0, so the truncation produced an empty string. Consumers such as `NavigationExpandingExpressionVisitor.CreateNavigationExpansionExpression` then crashed with `IndexOutOfRangeException` when indexing the result. Detect the synthesized prefix via the `<...>F` signature (`F` distinguishes file-scoped types from async state machines `<...>d__N` and local function host classes `<...>g__Local|N_N`) and skip past the `__` separator that appears after `>F<hex>`. The search for `__` is bounded to start after the closing `>`, so file names containing `__` are not misparsed; user type names containing `__` are preserved because the search uses the first occurrence within the suffix only. Fixes dotnet#32323
roji
requested changes
May 3, 2026
Member
roji
left a comment
There was a problem hiding this comment.
Thanks, this looks like a good fix - but can you fix DisplayName as well as ShortName (and add test coverage for that too)?
Extend the file-scoped type prefix stripping to DisplayName(), which previously surfaced Roslyn's raw <FileName>F<hex>__UserName synthesized name to users in error messages. Both ShortName() and DisplayName() now share a private static helper that scans the entire CLR display name and elides every <...>F<hex>__ sentinel, including occurrences nested inside generic argument lists. For example, List<<File>F1234__Inner> now becomes List<Inner> instead of being left ugly. Add positive theory rows covering nested generics, varying hex digest lengths, leading-underscore user names, and negative rows pinning that async state machines (>d), local functions (>g), closures (<>c), anonymous types (<>f), and lowercase-f Roslyn markers remain untouched.
roji
approved these changes
May 3, 2026
Member
roji
left a comment
There was a problem hiding this comment.
LGTM, @AndriySvyryd leaving to you in case you want to take a final look - otherwise I'll merge.
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 #32323.
Problem
file class/file recorddeclarations crash EF Core withIndexOutOfRangeExceptionwhen used as entity types:The same problem reproduces via
DbContext.Database.SqlQuery<MyEntity>(per @TwentyFourMinutes' comment on the issue).Root cause
The C# compiler synthesizes the metadata name for a file-scoped type as
<FileName>F<hex>__UserTypeName.IReadOnlyTypeBase.ShortNameusesIndexOf("<")to strip generic type arguments from the short display name. For file-scoped types the leading<of the synthesized prefix is at index0, soname[..0]produces an empty string and downstream consumers — for exampleNavigationExpandingExpressionVisitor.CreateNavigationExpansionExpressionatentityType.ShortName()[0].ToString().ToLowerInvariant()— crash on the indexer.Fix
Detect the synthesized file-scoped prefix specifically and skip past
__to expose the user-visible type name. The detection uses the<...>Fsignature, which distinguishes file-scoped types from other compiler-generated types whose names also begin with<:><>f__AnonymousType0(anonymous)<>prefix, handled by existing branch)<File>F<hex>__Type(file-scoped)FType<Method>d__0(async state machine)d<Method>g__Local|0_0(local function)gThe
__separator is searched starting after the closing>, so file names containing__are not misparsed. Once located, only the first__after the prefix is consumed, so user type names containing__are preserved.If any check in the new branch fails (e.g. no
>, signature is notF, no__follows),nameis left unchanged and falls through to the existing generic-stripping logic. The fix is a strict refinement, not a replacement.Tests
Added under
EntityTypeTest.ShortName_…:Positive — file-scoped synthesized names produce user-visible short names:
<Program>F1234ABCD__MyEntity→MyEntity<My__File>F1234ABCD__MyEntity→MyEntity(filename contains__)<Program>F1234ABCD__Foo__Bar→Foo__Bar(user name contains__)<Program>F1234ABCD__MyEntity<int>→MyEntity(file-scoped + generic)Negative — regular types are not touched by the new branch:
Foo→FooFoo__Bar→Foo__Bar(__alone is not a trigger)MyType<int>→MyType(existing generic strip)Foo__Bar<int>→Foo__Bar(combination)The three pre-existing
ShortName_on_compiler_generated_typeNtests covering<>-prefixed anonymous types continue to pass unchanged.Verification
dotnet build src/EFCore/EFCore.csproj— clean, 0 warningsdotnet test test/EFCore.Tests/EFCore.Tests.csproj— 6,804 / 6,804 pass (was 6,800; +4 new theory rows)dotnet test test/EFCore.InMemory.FunctionalTests/...— 27,592 / 27,592 pass (full query pipeline confirmed unaffected)What's intentionally not in this PR
The symptom site at
NavigationExpandingExpressionVisitor.CreateNavigationExpansionExpressionand the similar pattern at line 857 (methodCallExpression.Method.Name[0].ToString().ToLowerInvariant()) could also crash for synthesized method names, but those are out of scope here. This PR fixes the source — every consumer ofShortName()benefits without further changes. Happy to follow up on the method-name path in a separate PR if desired.