Create models and Clean Arch boilerplate
This commit is contained in:
parent
3926db5446
commit
872dc1e263
21 changed files with 576 additions and 27 deletions
37
CSR.Infrastructure/Persistence/CSRDbContext.cs
Normal file
37
CSR.Infrastructure/Persistence/CSRDbContext.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
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);
|
||||
|
||||
// --- Seed data --- //
|
||||
|
||||
var adminRole = new Role { Id = 1, Name = "Admin" };
|
||||
var userRole = new Role { Id = 2, Name = "User" };
|
||||
|
||||
modelBuilder.Entity<Role>()
|
||||
.HasData(adminRole, userRole);
|
||||
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue