diff --git a/src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs b/src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs index a65b4df63d1..0cee7132f4d 100644 --- a/src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs +++ b/src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs @@ -287,16 +287,14 @@ public static IEnumerable GetDataAnnotations( this INavigation navigation, IAnnotationCodeGenerator annotationCodeGenerator) { - if (navigation.IsOnDependent - && navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) + if (navigation.IsOnDependent) { yield return new AttributeCodeFragment( typeof(ForeignKeyAttribute), string.Join(", ", navigation.ForeignKey.Properties.Select(p => p.Name))); } - if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey() - && navigation.Inverse != null) + if (navigation.Inverse != null) { yield return new AttributeCodeFragment(typeof(InversePropertyAttribute), navigation.Inverse.Name); } @@ -312,14 +310,11 @@ public static IEnumerable GetDataAnnotations( this ISkipNavigation skipNavigation, IAnnotationCodeGenerator annotationCodeGenerator) { - if (skipNavigation.ForeignKey!.PrincipalKey.IsPrimaryKey()) - { - yield return new AttributeCodeFragment( - typeof(ForeignKeyAttribute), - string.Join(", ", skipNavigation.ForeignKey.Properties.Select(p => p.Name))); + yield return new AttributeCodeFragment( + typeof(ForeignKeyAttribute), + string.Join(", ", skipNavigation.ForeignKey.Properties.Select(p => p.Name))); - yield return new AttributeCodeFragment(typeof(InversePropertyAttribute), skipNavigation.Inverse.Name); - } + yield return new AttributeCodeFragment(typeof(InversePropertyAttribute), skipNavigation.Inverse.Name); } /// @@ -721,6 +716,7 @@ public static IEnumerable GetDataAnnotations( var hasForeignKey = new FluentApiCodeFragment(nameof(ReferenceReferenceBuilder.HasForeignKey)) { IsHandledByDataAnnotations = true }; + // HACK: Work around issue #29448 if (!foreignKey.PrincipalKey.IsPrimaryKey()) { hasForeignKey.IsHandledByDataAnnotations = false; diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs index 078fe2fc753..a7b0218bf7a 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs @@ -1575,7 +1575,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) }); [ConditionalFact] - public Task ForeignKeyAttribute_InversePropertyAttribute_is_not_generated_for_alternate_key() + public Task ForeignKeyAttribute_InversePropertyAttribute_when_composite_alternate_key() => TestAsync( modelBuilder => modelBuilder .Entity( @@ -1618,6 +1618,8 @@ public partial class Post public int? BlogId2 { get; set; } + [ForeignKey("BlogId1, BlogId2")] + [InverseProperty("Posts")] public virtual Blog BlogNavigation { get; set; } } """, @@ -1728,6 +1730,7 @@ public partial class Color public string ColorCode { get; set; } = null!; + [InverseProperty("Color")] public virtual ICollection Cars { get; } = new List(); } """, @@ -1750,6 +1753,8 @@ public partial class Car public string? ColorCode { get; set; } + [ForeignKey("ColorCode")] + [InverseProperty("Cars")] public virtual Color? Color { get; set; } } """, @@ -2797,6 +2802,8 @@ public partial class Blog public int Key { get; set; } + [ForeignKey("BlogsKey")] + [InverseProperty("Blogs")] public virtual ICollection Posts { get; } = new List(); } """,