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

[release/9.0] Avoid infinite recursion on identifying shadow FKs #34892

Merged
merged 1 commit into from
Oct 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private IConventionForeignKeyBuilder ProcessForeignKey(
&& fkProperty.ClrType.IsNullableType() == foreignKey.IsRequired
&& fkProperty.GetContainingForeignKeys().All(otherFk => otherFk.IsRequired == foreignKey.IsRequired))
{
var newType = fkProperty.ClrType.MakeNullable(!foreignKey.IsRequired);
var newType = fkProperty.ClrType.MakeNullable(!foreignKey.IsRequired && !fkProperty.IsKey());
if (fkProperty.ClrType != newType)
{
newProperties.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,61 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
}

[ConditionalFact]
public virtual void ManyToManyWithPayloadAndNavsToJoinClassShadowFKsTest()
=> Model101Test();

protected class ManyToManyWithPayloadAndNavsToJoinClassShadowFKs
{
public class Post
{
public int Id { get; set; }
public List<Tag> Tag { get; } = [];
public List<PostTag> PostTags { get; } = [];
}

public class Tag
{
public int Id { get; set; }
public List<Post> Post { get; } = [];
public List<PostTag> PostTags { get; } = [];
}

public class PostTag
{
}

public class Context0 : Context101
{
public DbSet<Post> Post
=> Set<Post>();

public DbSet<Tag> Tag
=> Set<Tag>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Post>()
.HasMany(e => e.Tag)
.WithMany(e => e.Post)
.UsingEntity<PostTag>(
l => l.HasOne<Tag>().WithMany(t => t.PostTags),
r => r.HasOne<Post>().WithMany(p => p.PostTags),
j => { });
}

public class Context1 : Context0
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Post>()
.HasMany(e => e.Tag)
.WithMany(e => e.Post)
.UsingEntity<PostTag>(
l => l.HasOne<Tag>().WithMany(t => t.PostTags).HasForeignKey("TagId"),
r => r.HasOne<Post>().WithMany(p => p.PostTags).HasForeignKey("PostId"),
j => { });
}
}

[ConditionalFact]
public virtual void ManyToManyWithNoCascadeDeleteTest()
=> Model101Test();
Expand Down