Skip to content

Commit

Permalink
Fix foreign key property change when foreign key is no longer in the …
Browse files Browse the repository at this point in the history
…model (#255)

Fixes #251
  • Loading branch information
roji authored Jan 5, 2024
1 parent d6f3811 commit ea2a909
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion EFCore.NamingConventions.Test/NameRewritingConventionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,23 @@ public void Alternative_key()
}

[Fact]
public void Foreign_key()
public void Foreign_key_collection_navigation()
{
var model = BuildModel(b => b.Entity<Blog>());
var entityType = model.FindEntityType(typeof(Post))!;
Assert.Equal("fk_post_blog_blog_id", entityType.GetForeignKeys().Single().GetConstraintName());
}

[Fact]
public void Foreign_key_reference_navigation()
{
var model = BuildModel(b => b.Entity<ReferenceNavigationPrincipal>());
var entityType = model.FindEntityType(typeof(ReferenceNavigationDependent))!;
Assert.Equal(
"fk_reference_navigation_dependent_reference_navigation_principal_principal_id",
entityType.GetForeignKeys().Single().GetConstraintName());
}

[Fact]
public void Index()
{
Expand Down Expand Up @@ -817,6 +827,19 @@ public class Location
public double Latitude { get; set; }
}

public class ReferenceNavigationPrincipal
{
public int Id { get; set; }
public ReferenceNavigationDependent Dependent { get; set; }

Check warning on line 833 in EFCore.NamingConventions.Test/NameRewritingConventionTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Dependent' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 833 in EFCore.NamingConventions.Test/NameRewritingConventionTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Dependent' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class ReferenceNavigationDependent
{
public int Id { get; set; }
public int PrincipalId { get; set; }
public ReferenceNavigationPrincipal Principal { get; set; }

Check warning on line 840 in EFCore.NamingConventions.Test/NameRewritingConventionTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Principal' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 840 in EFCore.NamingConventions.Test/NameRewritingConventionTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Principal' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class Board
{
public int Id { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void ProcessForeignKeyPropertiesChanged(
IConventionKey oldPrincipalKey,
IConventionContext<IReadOnlyList<IConventionProperty>> context)
{
if (relationshipBuilder.Metadata.GetDefaultName() is { } constraintName)
if (relationshipBuilder.Metadata.GetDefaultName() is { } constraintName && relationshipBuilder.Metadata.IsInModel)
{
relationshipBuilder.HasConstraintName(_namingNameRewriter.RewriteName(constraintName));
}
Expand Down

0 comments on commit ea2a909

Please sign in to comment.