CSR/CSR.Infrastructure/Persistence/CSRDbContext.cs

31 lines
857 B
C#

namespace CSR.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using CSR.Infrastructure.Persistence.Configurations;
public class CSRDbContext(DbContextOptions<CSRDbContext> options) : DbContext(options)
{
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new UserConfiguration());
modelBuilder.ApplyConfiguration(new RoleConfiguration());
modelBuilder.Entity<User>()
.HasIndex(u => u.Username)
.IsUnique();
modelBuilder.Entity<User>()
.HasOne(u => u.Role)
.WithMany(r => r.Users)
.HasForeignKey(u => u.RoleId);
modelBuilder.Entity<Role>()
.HasIndex(r => r.Name)
.IsUnique();
base.OnModelCreating(modelBuilder);
}
}