37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using backend.Auth;
|
|
using backend.Devices;
|
|
using backend.Settings;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace backend.Data;
|
|
|
|
public class AppDbContext(DbContextOptions<AppDbContext> options)
|
|
: IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>(options)
|
|
{
|
|
public DbSet<Device> Devices => Set<Device>();
|
|
public DbSet<DeviceConfig> DeviceConfigs => Set<DeviceConfig>();
|
|
public DbSet<SystemSettings> SystemSettings => Set<SystemSettings>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder b)
|
|
{
|
|
base.OnModelCreating(b);
|
|
|
|
b.Entity<Device>()
|
|
.HasOne<ApplicationUser>()
|
|
.WithMany()
|
|
.HasForeignKey(d => d.OwnerUserId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
b.Entity<Device>().HasIndex(d => d.TokenHash).IsUnique();
|
|
b.Entity<Device>()
|
|
.HasOne(d => d.Config)
|
|
.WithOne()
|
|
.HasForeignKey<DeviceConfig>(c => c.DeviceId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
b.Entity<DeviceConfig>().HasKey(c => c.DeviceId);
|
|
b.Entity<SystemSettings>().HasKey(s => s.Id);
|
|
b.Entity<SystemSettings>().Property(s => s.Id).ValueGeneratedNever();
|
|
}
|
|
}
|