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 5.0 Many-to-Many generating singular table names and plural column names #23258

Closed
mhosman opened this issue Nov 10, 2020 · 8 comments
Closed

Comments

@mhosman
Copy link

mhosman commented Nov 10, 2020

Example:

public class Customer
{
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Color> Colors { get; set; }
}

public class Color
{
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Customer> Customers { get; set; }
}

This is generating a table CustomerColor (singular) with column names in plural: ColorsId, CustomersId

@ajcvickers
Copy link
Contributor

@mhosman EF isn't actually pluralizing or singularizing anything here. The table names come from your entity type names. (If you had DbSet properties for these types, then the names would come from those.) The FK names are based on the navigation properties on each end of the relationship.

@mhosman
Copy link
Author

mhosman commented Nov 10, 2020

Thanks for the response. There's a way to change this manually? Like in fluent API? Thanks!

@ajcvickers
Copy link
Contributor

Yes: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#many-to-many

@mhosman
Copy link
Author

mhosman commented Nov 11, 2020

Sorry @ajcvickers, not able to find how to change column names without specifying the CLR class (only was able to find a way to change the table name in the docs, no the column names).

Anyway... this behaviour is kind of weird... Since this are always collections, if resulting values are based on the navigation properties, you'll always get pluralized values (unless you name a collection as singular).

Also, in the docs, you have pluralized values in your classes, but the resulting SQL it's okay (singular). So... I don't understand where is the issue.

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Nov 12, 2020

@mhosman You need to use this overload of UsingEntity:

        modelBuilder.Entity<Customer>()
            .HasMany(p => p.Colors)
            .WithMany(p => p.Customers)
            .UsingEntity<Dictionary<string, object>>(
                "CustomerColor",
                j => j
                    .HasOne<Color>()
                    .WithMany()
                    .HasForeignKey("ColorId"),
                j => j
                    .HasOne<Customer>()
                    .WithMany()
                    .HasForeignKey("CustomerId"));

We use the navigation names in the generated names to make it easier to distinguish them for self-referencing relationships and have a simple naming rule.

@mhosman
Copy link
Author

mhosman commented Nov 12, 2020

Great @AndriySvyryd thank you very much!! Worked like a charm. Closing!!

@mhosman mhosman closed this as completed Nov 12, 2020
@ajcvickers
Copy link
Contributor

@AndriySvyryd Do we have anything tracking making this easier? I don't (yet) think we made the wrong choice on FK property names, but I do think that many people will want to change them.

@ajcvickers ajcvickers reopened this Nov 12, 2020
@AndriySvyryd
Copy link
Member

@ajcvickers #21535

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
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

3 participants