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

EF Core 7 Scaffold-DbContext pluralizer incorrectly removes .ToTable() in many-to-many relationship #29977

Closed
jornhd opened this issue Jan 4, 2023 · 2 comments

Comments

@jornhd
Copy link

jornhd commented Jan 4, 2023

We use Scaffold-DbContext to generate entity classes and db context from a legacy SQLServer database.
Several of the tables in the database ends with a plural 's', including the table "CarDrivers" containing a many-to-many relationship between tables "Car" and "Driver".
The pluralizer removes this ending 's' from table names when creating entity classes, and that's fine.
But when generating the many-to-many relationship, this now causes an error.
In EF Core 6, .ToTable("CarDrivers") was added that made this work, but now in EF Core 7 this ToTable is removed causing SQLException Invalid object name 'CarDriver'

The many-to-many relation generated with EF Core 6:

  modelBuilder.Entity<Car>(entity =>
  {
      _(snip)_

      entity.HasMany(d => d.Drivers)
          .WithMany(p => p.Cars)
          .UsingEntity<Dictionary<string, object>>(
              "CarDriver",
              l => l.HasOne<Driver>().WithMany().HasForeignKey("DriverId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_CarDrivers_Driver"),
              r => r.HasOne<Car>().WithMany().HasForeignKey("CarId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_CarDrivers_Car"),
              j =>
              {
                  j.HasKey("CarId", "DriverId");
                  j.ToTable("CarDrivers");
              });
    });

The many-to-many relation generated with EF Core 7:

  modelBuilder.Entity<Car>(entity =>
  {
      _(snip)_

      entity.HasMany(d => d.Drivers).WithMany(p => p.Cars)
        .UsingEntity<Dictionary<string, object>>(
            "CalendarResource",
            r => r.HasOne<Driver>().WithMany()
                .HasForeignKey("DriverId")
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_CarDrivers_Driver"),
            l => l.HasOne<Car>().WithMany()
                .HasForeignKey("CarId")
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_CarDrivers_Car"),
            j =>
            {
                j.HasKey("CarId", "DriverId");
            });
    });

Note the j.ToTable("CarDrivers"); is missing in the EF Core 7 code.

By adding -NoPluralize in the Scaffold-DbContext command line, the generated code is correct, as the joinEntityName is set to "CarDrivers" instead of "CarDriver", but then many of the class names and collection properties changes, causing lots of errors in the code base, so this is not a desirable workaround.

@ajcvickers
Copy link
Member

ajcvickers commented Jan 4, 2023

Duplicate of #29634

@ajcvickers ajcvickers marked this as a duplicate of #29634 Jan 4, 2023
@jornhd
Copy link
Author

jornhd commented Jan 4, 2023

Actually it was closer to #29544 but glad to see it's already fixed in 7.0.3. :)

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants