Skip to content

Commit

Permalink
Fix for the optimizer when an entity has a collection property that r…
Browse files Browse the repository at this point in the history
…eferences itself.

Fixes #27301
# Conflicts:
#	src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs
  • Loading branch information
leniency authored and AndriySvyryd committed Feb 7, 2022
1 parent a170463 commit 4f1aa64
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ private static void AddNamespace(Type type, ISet<string> namespaces)
}

var sequenceType = type.TryGetSequenceType();
if (sequenceType != null)
if (sequenceType != null && sequenceType != type)
{
AddNamespace(sequenceType, namespaces);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
public class CSharpRuntimeModelCodeGeneratorTest
{

[ConditionalFact]
public void Self_referential_property()
=> Test(
new TestModel.Internal.SelfReferentialDbContext(),
new CompiledModelCodeGenerationOptions(),
assertModel: model =>
{
Assert.Single(model.GetEntityTypes());
Assert.Same(model, model.FindRuntimeAnnotationValue("ReadOnlyModel"));
}
);


[ConditionalFact]
public void Empty_model()
{
Expand Down Expand Up @@ -3659,6 +3673,18 @@ public class IdentityUser : TestModels.AspNetIdentity.IdentityUser
{

}


public class SelfReferentialEntity
{
public long Id { get; set; }

public SelfReferentialProperty Collection { get; set; }
}

public class SelfReferentialProperty : List<SelfReferentialProperty>
{
}
}

namespace Microsoft.EntityFrameworkCore.Scaffolding.TestModel.Internal
Expand All @@ -3678,4 +3704,29 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Scaffolding.Internal.Internal>();
}
}

public class SelfReferentialDbContext : ContextBase
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Scaffolding.Internal.SelfReferentialEntity>(
eb =>
{
eb.Property(e => e.Collection).HasConversion(typeof(SelfReferentialPropertyValueConverter));
});
}
}

public class SelfReferentialPropertyValueConverter : ValueConverter<Scaffolding.Internal.SelfReferentialProperty, string>
{
public SelfReferentialPropertyValueConverter()
: this(null)
{ }

public SelfReferentialPropertyValueConverter(ConverterMappingHints hints)
: base(v => null, v => null, hints)
{ }
}
}

0 comments on commit 4f1aa64

Please sign in to comment.