Skip to content

Commit

Permalink
Scaffold [ForeignKey] and [InverseProperty] when principal key is alt…
Browse files Browse the repository at this point in the history
…ernate key
  • Loading branch information
bricelam committed Dec 1, 2022
1 parent db7ef3b commit 4b72fd5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/EFCore.Design/Extensions/ScaffoldingModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,14 @@ public static IEnumerable<AttributeCodeFragment> 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);
}
Expand All @@ -312,14 +310,11 @@ public static IEnumerable<AttributeCodeFragment> 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);
}

/// <summary>
Expand Down Expand Up @@ -720,6 +715,7 @@ public static IEnumerable<AttributeCodeFragment> GetDataAnnotations(
var hasForeignKey =
new FluentApiCodeFragment(nameof(ReferenceReferenceBuilder.HasForeignKey)) { IsHandledByDataAnnotations = true };

// HACK: Work around issue #29448
if (!foreignKey.PrincipalKey.IsPrimaryKey())
{
hasForeignKey.IsHandledByDataAnnotations = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,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(
Expand Down Expand Up @@ -1592,6 +1592,8 @@ public partial class Post
public int? BlogId2 { get; set; }
[ForeignKey(""BlogId1, BlogId2"")]
[InverseProperty(""Posts"")]
public virtual Blog BlogNavigation { get; set; }
}
",
Expand Down Expand Up @@ -1702,6 +1704,7 @@ public partial class Color
public string ColorCode { get; set; } = null!;
[InverseProperty(""Color"")]
public virtual ICollection<Car> Cars { get; } = new List<Car>();
}
",
Expand All @@ -1723,6 +1726,8 @@ public partial class Car
public string? ColorCode { get; set; }
[ForeignKey(""ColorCode"")]
[InverseProperty(""Cars"")]
public virtual Color? Color { get; set; }
}
",
Expand Down Expand Up @@ -2666,6 +2671,8 @@ public partial class Blog
public int Key { get; set; }
[ForeignKey(""BlogsKey"")]
[InverseProperty(""Blogs"")]
public virtual ICollection<Post> Posts { get; } = new List<Post>();
}
",
Expand Down

0 comments on commit 4b72fd5

Please sign in to comment.