You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CREATETABLEBlogs (Id int IDENTITY CONSTRAINT [PK_Blogs] PRIMARY KEY)
go
CREATETABLEPosts (Id int IDENTITY CONSTRAINT [PK_Posts] PRIMARY KEY)
go
CREATETABLEBlogPost (
Blog_Id intNOT NULLCONSTRAINT [FK_BlogPost_Blogs] REFERENCES Blogs ON DELETE CASCADE,
Post_Id intNOT NULLCONSTRAINT [FK_BlogPost_Posts] REFERENCES Posts ON DELETE CASCADE,
CONSTRAINT [PK_BlogPost] PRIMARY KEY (Blog_Id, Post_Id)
)
go
(Note the field names are Blog_Id and Post_Id, also the name of the ManyToMany table is singular to avoid #29544)
When scaffolding in EF7 the following Configuration will be created.
modelBuilder.Entity<Blog>(entity =>{entity.HasMany(d =>d.Posts).WithMany(p =>p.Blogs).UsingEntity<Dictionary<string,object>>("BlogPost",
r =>r.HasOne<Post>().WithMany().HasForeignKey("PostId").HasConstraintName("FK_BlogPost_Posts"),
l =>l.HasOne<Blog>().WithMany().HasForeignKey("BlogId").HasConstraintName("FK_BlogPost_Blogs"),
j =>{j.HasKey("BlogId","PostId");j.ToTable("BlogPost");});});
SELECT [t].[Id]
FROM [Blogs] AS [b]
INNER JOIN (
SELECT [p].[Id], [b0].[BlogId]
FROM [BlogPost] AS [b0]
INNER JOIN [Posts] AS [p] ON [b0].[PostId] = [p].[Id] -- PostId is invalid name should be Post_Id
) AS [t] ON [b].[Id] = [t].[BlogId]
Which results in an exception: Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'PostId'.
Assume the following database
(Note the field names are Blog_Id and Post_Id, also the name of the ManyToMany table is singular to avoid #29544)
When scaffolding in EF7 the following Configuration will be created.
When executing the following statement:
The following SQL will be generated:
Which results in an exception:
Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'PostId'.
EF Core version: 7.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 7.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)
The text was updated successfully, but these errors were encountered: