-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Query with n:m relation adds non-existent column #26590
Comments
I had the same issue when I updated my project from efcore5 to efcore6. // On Order (parent):
// This was a duplicate, removing it solved the problem
// The "phantom" property for me was named OrderRowId, that was wrong anyway as it ignored the explicit mapping specified with .HasForeignKey()
builder.HasMany(e => e.DetailsPrice).WithOne()
.HasForeignKey(e => e.OrderNumber).HasPrincipalKey(e => e.OrderNumber);
...
// On Detail: (the one that i kept in my mapping)
builder.HasOne(e => e.Order).WithMany(e => e.DetailsPrice)
.HasForeignKey(e => e.OrderNumber).HasPrincipalKey(e => e.OrderNumber); Please check your mapping, maybe the scaffolder made a similar mistake |
Thanks for looking into this. I didn't touch the Model code at all here's what was scaffolded: protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseCollation("SQL_Latin1_General_CP1_CI_AS");
modelBuilder.Entity<Crashtest>(entity =>
{
entity.HasMany(d => d.Karosserieform)
.WithMany(p => p.Crashtest)
.UsingEntity<Dictionary<string, object>>(
"CrashtestKarosserieform",
l => l.HasOne<Karosserieform>().WithMany().HasForeignKey("KarosserieformId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("fk_kf"),
r => r.HasOne<Crashtest>().WithMany().HasForeignKey("CrashtestId").HasConstraintName("fk_ct"),
j =>
{
j.HasKey("CrashtestId", "KarosserieformId").HasName("pk_link");
j.ToTable("CrashtestKarosserieform");
});
});
OnModelCreatingPartial(modelBuilder);
} so I don't think this is the issue. |
Min repro: public partial class Blog
{
public int BlogId { get; set; }
[ForeignKey("BlogId")]
public virtual ICollection<Post> Posts { get; set; }
}
public partial class Post
{
public int PostId { get; set; }
[ForeignKey("PostId")]
public virtual ICollection<Blog> Blogs { get; set; }
}
modelBuilder.Entity<Blog>(entity =>
{
entity.HasMany(d => d.Posts)
.WithMany(p => p.Blogs)
.UsingEntity<Dictionary<string, object>>(
"BlogPost",
l => l.HasOne<Post>().WithMany().HasForeignKey("PostId"),
r => r.HasOne<Blog>().WithMany().HasForeignKey("BlogId"),
j =>
{
j.HasKey("BlogId", "PostId");
j.ToTable("BlogPost");
});
}); Remove either FKAttributes or manual configuration and it generates correct model.
The join entity has unused property |
cc: @AndriySvyryd |
FYI for those impacted by this issue: EF Core 6.0.1 is now available from NuGet. |
#n:m relation query statements build incorrectly
when including data from a n:m table the SQL generated includes a non-existent column, resulting in a failing SQL statement
the below code is created by dbScaffold (DDL statements at the bottom)
this builds the following SQL statement which fails because there is no
CrashtestId1
columnStatement used for scaffolding
DDL-Statements:
provider and version information
EF Core version: 6.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system: win 10
IDE: (e.g. Visual Studio 20202 current)
The text was updated successfully, but these errors were encountered: