diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs index 2e1a73e86f5..81356d1d5c4 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs @@ -1400,7 +1400,7 @@ private static void AddNamespace(Type type, ISet namespaces) } var sequenceType = type.TryGetSequenceType(); - if (sequenceType != null) + if (sequenceType != null && sequenceType != type) { AddNamespace(sequenceType, namespaces); } diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpRuntimeModelCodeGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpRuntimeModelCodeGeneratorTest.cs index 0994a8bb75b..af66a4e5a16 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpRuntimeModelCodeGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpRuntimeModelCodeGeneratorTest.cs @@ -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() { @@ -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 + { + } } namespace Microsoft.EntityFrameworkCore.Scaffolding.TestModel.Internal @@ -3678,4 +3704,29 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity(); } } + + public class SelfReferentialDbContext : ContextBase + { + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity( + eb => + { + eb.Property(e => e.Collection).HasConversion(typeof(SelfReferentialPropertyValueConverter)); + }); + } + } + + public class SelfReferentialPropertyValueConverter : ValueConverter + { + public SelfReferentialPropertyValueConverter() + : this(null) + { } + + public SelfReferentialPropertyValueConverter(ConverterMappingHints hints) + : base(v => null, v => null, hints) + { } + } }