efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Invalid singularization when scaffolding a manytomany table

Open MariovanZeist opened this issue 4 years ago • 9 comments

Assume the following Database:

CREATE TABLE Blogs (Id int IDENTITY CONSTRAINT [PK_Blogs] PRIMARY KEY,) 
go 

CREATE TABLE Posts (Id int IDENTITY CONSTRAINT [PK_Posts] PRIMARY KEY,) 
go 

CREATE TABLE BlogPosts (
  BlogId int NOT NULL CONSTRAINT [FK_BlogPosts_Blogs] REFERENCES Blogs ON DELETE CASCADE,
  PostId int NOT NULL CONSTRAINT [FK_BlogPosts_Posts] REFERENCES Posts ON DELETE CASCADE,
  CONSTRAINT [PK_BlogPosts ] PRIMARY KEY (BlogId, PostId)
)
go 

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",       //<-note the missing s from the original Tablename(BlogPosts)
             r => r.HasOne<Post>().WithMany()
                 .HasForeignKey("PostId")
                 .HasConstraintName("FK_BlogPosts_Posts"),
             l => l.HasOne<Blog>().WithMany()
                 .HasForeignKey("BlogId")
                 .HasConstraintName("FK_BlogPosts_Blogs"),
             j =>
             {
                 j.HasKey("BlogId", "PostId").HasName("PK_BlogPosts ");
                 //The following line of code is NOT generated (but was generated in earlier versions) and might be what is actually missing here
                 //j.ToTable("BlogPosts");
             });
 });

When executing the following statement:

  var allposts = await ctx.Blogs.SelectMany(b => b.Posts).ToArrayAsync();

The following SQL will be generated:

SELECT [t].[Id]
FROM [Blogs] AS [b]
INNER JOIN (
    SELECT [p].[Id], [b0].[BlogId]
    FROM [BlogPost] AS [b0]    --  <-Invalid table name
    INNER JOIN [Posts] AS [p] ON [b0].[PostId] = [p].[Id]
) AS [t] ON [b].[Id] = [t].[BlogId]

resulting in Microsoft.Data.SqlClient.SqlException: 'Invalid object name 'BlogPost'.'

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)

MariovanZeist avatar Nov 12 '22 20:11 MariovanZeist

Is you model configuration being loaded?

Which command do you run exactly?

ErikEJ avatar Nov 13 '22 06:11 ErikEJ

@ErikEJ I am running the following command: dotnet ef dbcontext scaffold "Data Source=localhost;Initial Catalog=EFCoreBugs;Integrated Security=True;Encrypt=False" Microsoft.EntityFrameworkCore.SqlServer -f --no-onconfiguring --no-build -c EFDbContext

And if you mean by if my model get's loaded that the above configuration is used, then yes it is.

also running dotnet-ef --version will return: 7.0.0

MariovanZeist avatar Nov 13 '22 11:11 MariovanZeist

Thanks, I am able to repro

ErikEJ avatar Nov 14 '22 14:11 ErikEJ

/cc @bricelam We should consider patching this.

ajcvickers avatar Nov 15 '22 15:11 ajcvickers

Note from triage: prepare a patch for this.

ajcvickers avatar Nov 17 '22 11:11 ajcvickers

not sure if ite helps, but i have this issue too. after upgrading from ef core 6 to ef core 7 scaffolding generates a different result. j.ToTable("TableName"); got replace with j.ToTable(tb => { tb.HasTrigger("TableName_some_trigger"); //we use triggers });

some queries stopped working because of this change.

taipignas avatar Nov 30 '22 13:11 taipignas

@taipignas Can you file a new issue with more information? That looks like a different bug.

bricelam avatar Dec 07 '22 18:12 bricelam

🩹 Workaround

Unfortunately, the only good workaround here is to manually add the ToTable call after the code is generated.

bricelam avatar Dec 08 '22 18:12 bricelam

@bricelam As a workaround for this issue and some others (#29563 for example), I turned to the new T4 templates. (awesome addition to efcore) They were a live saver as they allowed me to circumvent these issues and allowed me to migrate to net7.

MariovanZeist avatar Dec 08 '22 19:12 MariovanZeist