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 Jan 6, 2023
1 parent aaa990b commit 9928867
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 @@ -721,6 +716,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 @@ -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(
Expand Down Expand Up @@ -1618,6 +1618,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 @@ -1728,6 +1730,7 @@ public partial class Color
public string ColorCode { get; set; } = null!;
[InverseProperty("Color")]
public virtual ICollection<Car> Cars { get; } = new List<Car>();
}
""",
Expand All @@ -1750,6 +1753,8 @@ public partial class Car
public string? ColorCode { get; set; }
[ForeignKey("ColorCode")]
[InverseProperty("Cars")]
public virtual Color? Color { get; set; }
}
""",
Expand Down Expand Up @@ -2797,6 +2802,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 9928867

Please sign in to comment.