File tree Expand file tree Collapse file tree 2 files changed +59
-1
lines changed
src/EFCore/Metadata/Conventions
test/EFCore.Specification.Tests Expand file tree Collapse file tree 2 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,9 @@ public class ForeignKeyPropertyDiscoveryConvention :
50
50
IPropertyFieldChangedConvention ,
51
51
IModelFinalizingConvention
52
52
{
53
+ private static readonly bool UseOldBehavior34875 =
54
+ AppContext . TryGetSwitch ( "Microsoft.EntityFrameworkCore.Issue34875" , out var enabled34875 ) && enabled34875 ;
55
+
53
56
/// <summary>
54
57
/// Creates a new instance of <see cref="ForeignKeyPropertyDiscoveryConvention" />.
55
58
/// </summary>
@@ -142,7 +145,7 @@ private IConventionForeignKeyBuilder ProcessForeignKey(
142
145
&& fkProperty . ClrType . IsNullableType ( ) == foreignKey . IsRequired
143
146
&& fkProperty . GetContainingForeignKeys ( ) . All ( otherFk => otherFk . IsRequired == foreignKey . IsRequired ) )
144
147
{
145
- var newType = fkProperty . ClrType . MakeNullable ( ! foreignKey . IsRequired ) ;
148
+ var newType = fkProperty . ClrType . MakeNullable ( ! foreignKey . IsRequired && ( ! fkProperty . IsKey ( ) || UseOldBehavior34875 ) ) ;
146
149
if ( fkProperty . ClrType != newType )
147
150
{
148
151
fkProperty . DeclaringType . Builder . Property (
Original file line number Diff line number Diff line change @@ -737,6 +737,61 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
737
737
}
738
738
}
739
739
740
+ [ ConditionalFact ]
741
+ public virtual void ManyToManyWithPayloadAndNavsToJoinClassShadowFKsTest ( )
742
+ => Model101Test ( ) ;
743
+
744
+ protected class ManyToManyWithPayloadAndNavsToJoinClassShadowFKs
745
+ {
746
+ public class Post
747
+ {
748
+ public int Id { get ; set ; }
749
+ public List < Tag > Tag { get ; } = [ ] ;
750
+ public List < PostTag > PostTags { get ; } = [ ] ;
751
+ }
752
+
753
+ public class Tag
754
+ {
755
+ public int Id { get ; set ; }
756
+ public List < Post > Post { get ; } = [ ] ;
757
+ public List < PostTag > PostTags { get ; } = [ ] ;
758
+ }
759
+
760
+ public class PostTag
761
+ {
762
+ }
763
+
764
+ public class Context0 : Context101
765
+ {
766
+ public DbSet < Post > Post
767
+ => Set < Post > ( ) ;
768
+
769
+ public DbSet < Tag > Tag
770
+ => Set < Tag > ( ) ;
771
+
772
+ protected override void OnModelCreating ( ModelBuilder modelBuilder )
773
+ => modelBuilder . Entity < Post > ( )
774
+ . HasMany ( e => e . Tag )
775
+ . WithMany ( e => e . Post )
776
+ . UsingEntity < PostTag > (
777
+ l => l . HasOne < Tag > ( ) . WithMany ( t => t . PostTags ) ,
778
+ r => r . HasOne < Post > ( ) . WithMany ( p => p . PostTags ) ,
779
+ j => { } ) ;
780
+ }
781
+
782
+ public class Context1 : Context0
783
+ {
784
+ protected override void OnModelCreating ( ModelBuilder modelBuilder )
785
+ => modelBuilder . Entity < Post > ( )
786
+ . HasMany ( e => e . Tag )
787
+ . WithMany ( e => e . Post )
788
+ . UsingEntity < PostTag > (
789
+ l => l . HasOne < Tag > ( ) . WithMany ( t => t . PostTags ) . HasForeignKey ( "TagId" ) ,
790
+ r => r . HasOne < Post > ( ) . WithMany ( p => p . PostTags ) . HasForeignKey ( "PostId" ) ,
791
+ j => { } ) ;
792
+ }
793
+ }
794
+
740
795
[ ConditionalFact ]
741
796
public virtual void ManyToManyWithNoCascadeDeleteTest ( )
742
797
=> Model101Test ( ) ;
You can’t perform that action at this time.
0 commit comments