diff --git a/src/EFCore/Metadata/Conventions/ForeignKeyPropertyDiscoveryConvention.cs b/src/EFCore/Metadata/Conventions/ForeignKeyPropertyDiscoveryConvention.cs index 167605a09d2..45194d0a2a1 100644 --- a/src/EFCore/Metadata/Conventions/ForeignKeyPropertyDiscoveryConvention.cs +++ b/src/EFCore/Metadata/Conventions/ForeignKeyPropertyDiscoveryConvention.cs @@ -141,7 +141,7 @@ private IConventionForeignKeyBuilder ProcessForeignKey( && fkProperty.ClrType.IsNullableType() == foreignKey.IsRequired && fkProperty.GetContainingForeignKeys().All(otherFk => otherFk.IsRequired == foreignKey.IsRequired)) { - var newType = fkProperty.ClrType.MakeNullable(!foreignKey.IsRequired); + var newType = fkProperty.ClrType.MakeNullable(!foreignKey.IsRequired && !fkProperty.IsKey()); if (fkProperty.ClrType != newType) { newProperties.Add( diff --git a/test/EFCore.Specification.Tests/ModelBuilding101ManyToManyTestBase.cs b/test/EFCore.Specification.Tests/ModelBuilding101ManyToManyTestBase.cs index 6a30a4dbcb0..b22946a0d46 100644 --- a/test/EFCore.Specification.Tests/ModelBuilding101ManyToManyTestBase.cs +++ b/test/EFCore.Specification.Tests/ModelBuilding101ManyToManyTestBase.cs @@ -735,6 +735,61 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) } } + [ConditionalFact] + public virtual void ManyToManyWithPayloadAndNavsToJoinClassShadowFKsTest() + => Model101Test(); + + protected class ManyToManyWithPayloadAndNavsToJoinClassShadowFKs + { + public class Post + { + public int Id { get; set; } + public List Tag { get; } = []; + public List PostTags { get; } = []; + } + + public class Tag + { + public int Id { get; set; } + public List Post { get; } = []; + public List PostTags { get; } = []; + } + + public class PostTag + { + } + + public class Context0 : Context101 + { + public DbSet Post + => Set(); + + public DbSet Tag + => Set(); + + protected override void OnModelCreating(ModelBuilder modelBuilder) + => modelBuilder.Entity() + .HasMany(e => e.Tag) + .WithMany(e => e.Post) + .UsingEntity( + l => l.HasOne().WithMany(t => t.PostTags), + r => r.HasOne().WithMany(p => p.PostTags), + j => { }); + } + + public class Context1 : Context0 + { + protected override void OnModelCreating(ModelBuilder modelBuilder) + => modelBuilder.Entity() + .HasMany(e => e.Tag) + .WithMany(e => e.Post) + .UsingEntity( + l => l.HasOne().WithMany(t => t.PostTags).HasForeignKey("TagId"), + r => r.HasOne().WithMany(p => p.PostTags).HasForeignKey("PostId"), + j => { }); + } + } + [ConditionalFact] public virtual void ManyToManyWithNoCascadeDeleteTest() => Model101Test();