Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold [ForeignKey] and [InverseProperty] when principal key is alternate key #29449

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 bool IsHandledByDataAnnotations(this IIndex index, IAnnotationCode
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 bool IsHandledByDataAnnotations(this IIndex index, IAnnotationCode
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 @@ valueGenerated switch
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