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 for the optimizer when an entity has a collection property that references itself. #27350

Merged
merged 2 commits into from
Feb 4, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,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 @@ -41,6 +41,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()
=> Test(
Expand Down Expand Up @@ -3648,6 +3662,18 @@ public class Index
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 @@ -3668,4 +3694,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)
{ }
}
}