skip to Main Content

EF core keeps making up table names and columns?

Table 'player_roles' doesn't exist

Bare in mind I’m using snake_case column naming.

I have 5 tables

  • players
  • players_roles (bridge table)
  • roles
  • roles_permissions (bridge table)
  • permissions

With the following entities:

public class Role
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Player> Players { get; set; }
    public List<Permission> Permissions { get; set; }
}

public class Permission
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Player
{
    public int Id { get; set; }
    public List<Role> Roles { get; set; }
}

And configuration

modelBuilder.Entity<Player>()
    .HasMany(r => r.Roles)
    .WithMany(p => p.Players)
    .UsingEntity("players_roles");

Yet EF is trying to query player_roles?

Raw SQL

LEFT JOIN (
    SELECT `p10`.`players_id`, `p10`.`roles_id`, `p11`.`id`, `p11`.`name`
    FROM `players_roles` AS `p10`
    INNER JOIN `player_roles` AS `p11` ON `p10`.`roles_id` = `p11`.`id`
) AS `t0` ON `t`.`id` = `t0`.`players_id`

2

Answers


  1. This doesn’t answer your question, but my advice is you solve it by making the class

    [Table("players_roles"]
    public class PlayersRoles
    

    With the keys etc. Thereby avoiding the fluent api.

    The answer to why it happens would be interesting though. EF Core have some magic in regards to plural names so my guess is that it comes from some of that.

    Login or Signup to reply.
  2. I think there is a name mismatch between your entities and the database table names.

    In your code, you used the UsingEntity method with the name "players_roles", which is correct according to your table.
    But EF Core creates the join table names by concatenating the two related entity names in alphabetical order. This is why it’s generating the name "player_roles" instead of "players_roles".

    Or you can use the TotTable method, that is, replace the last line in your configuration with:

    .UsingEntity(j => j.ToTable("players_roles"));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search