Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to #30115 - IndexOutOfRangeException in CreateNavigationExpansionExpression(Expression sourceExpression, IEntityType entityType) / get_Chars(Int32 index) when EF class named "<>f__AnonymousType01Child" in 7.0.2 version #30537

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/EFCore/Metadata/IReadOnlyTypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ string ShortName()
if (!HasSharedClrType)
{
var name = ClrType.ShortDisplayName();
if (name.StartsWith("<>", StringComparison.Ordinal))
{
name = name[2..];
}

var lessIndex = name.IndexOf("<", StringComparison.Ordinal);
if (lessIndex == -1)
{
Expand Down
51 changes: 51 additions & 0 deletions test/EFCore.Tests/Metadata/Internal/EntityTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.Globalization;
using System.Reflection.Emit;

// ReSharper disable CollectionNeverUpdated.Local
// ReSharper disable UnusedAutoPropertyAccessor.Local
Expand Down Expand Up @@ -2959,6 +2960,56 @@ public void Collections_dont_have_relationship_indexes_when_full_notifications_w
Assert.Equal(3, entityType.RelationshipPropertyCount());
}

[ConditionalFact]
public void ShortName_on_compiler_generated_type1()
{
var model = CreateModel();

var typeName = "<>f__AnonymousType01Child";
model.AddEntityType(typeName);
var entityType = model.FinalizeModel().FindEntityType(typeName);

Assert.Equal(typeName, entityType.ShortName());
}

[ConditionalFact]
public void ShortName_on_compiler_generated_type2()
{
var model = CreateModel();

var typeName = "<>f__AnonymousType01Child";
var assemblyName = new AssemblyName("DynamicEntityClrTypeAssembly");
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");
var typeBuilder = moduleBuilder.DefineType(typeName);
var type = typeBuilder.CreateType();

model.AddEntityType(type);

var entityType = model.FinalizeModel().FindEntityType(typeName);

Assert.Equal(typeName[2..], entityType.ShortName());
}

[ConditionalFact]
public void ShortName_on_compiler_generated_type3()
{
var model = CreateModel();

var typeName = "<>__AnonymousType01Child<int>";
var assemblyName = new AssemblyName("DynamicEntityClrTypeAssembly");
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");
var typeBuilder = moduleBuilder.DefineType(typeName);
var type = typeBuilder.CreateType();

model.AddEntityType(type);

var entityType = model.FinalizeModel().FindEntityType(typeName);

Assert.Equal("__AnonymousType01Child", entityType.ShortName());
}

private readonly IMutableModel _model = BuildModel();

private IMutableEntityType DependentType
Expand Down