Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Many-to-Many Relation Table Naming [duplicate]

Does EF Core provide a way of naming the many-to-many relations mapping to database tables ?

In a code-first pattern, I have the following 2 Entities:

[Table("Prefix.Users")]
public class User
{
    public int ID { get; set; }
    public IEnumerable<Role> Roles { get; set; }
}


[Table("Prefix.Roles")]
public class Role
{
    public int ID { get; set; }
    public IEnumerable<User> Users { get; set; }
}

I've skipped the detailed Entity structure here. The ID properties in User & Role are keys (Database generated Identity)

User and Role entities share a many-to-many relationship.

EF Core generates a third table in Database with Table name UsersRoles

Is there a way I can add a prefix to the 3rd table name so it becomes Prefix.UsersRoles without manually adding a third Entity UserRoles that maps User and Role and giving it the desired name with Prefix

like image 509
Naveen Avatar asked Sep 06 '25 03:09

Naveen


1 Answers

Use fluent API instead of using data annotations

Your model classes should be like this.

 public class User
 {
    public int Id { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }

     public virtual ICollection<UserRole> UserRoles { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual ICollection<UserRole> UserRoles { get; set; }
}

public class UserRole
{
    public int UserId { get; set; }
    public int RoleId { get; set; }

    public virtual User User { get; set; }
    public virtual Role Role { get; set; }

}

Your fluent api configuration classes like be this

public class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.ToTable("User");

        builder.HasKey(x => x.Id);
    }
}
public class RoleConfiguration : IEntityTypeConfiguration<Role>
{
    public void Configure(EntityTypeBuilder<Role> builder)
    {
        builder.ToTable("Role");

        builder.HasKey(x => x.Id);
    }
}

public class UserRoleConfiguration : IEntityTypeConfiguration<UserRole>
{
    public void Configure(EntityTypeBuilder<UserRole> builder)
    {
        builder.ToTable("UserRole");

        builder.HasKey(x => new { x.UserId, x.RoleId });

        builder
            .HasOne<Role>(s => s.Role)
            .WithMany(r => r.UserRoles)
            .HasForeignKey(s => s.RoleId).OnDelete(DeleteBehavior.Restrict);

        builder
            .HasOne<User>(s => s.User)
            .WithMany(r => r.UserRoles)
            .HasForeignKey(s => s.UserId).OnDelete(DeleteBehavior.Restrict);

    }
}

Your DbContext class should be like this

public class MyDbContext : DbContext
{
    public EEGDbContext()
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=xxxx;Database=DB;User Id=sa;Password=xxxxx;");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new UserConfiguration());
        modelBuilder.ApplyConfiguration(new RoleConfiguration());
        modelBuilder.ApplyConfiguration(new UserRoleConfiguration());

        base.OnModelCreating(modelBuilder);
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<UserRole> UserRoles { get; set; }

}
like image 178
Erandika Sandaruwan Avatar answered Sep 08 '25 22:09

Erandika Sandaruwan