2026-01-13 13:50:27 +08:00
|
|
|
using DRS9.Dashboard.Domain.Entities;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace DRS9.Dashboard.Infrastructure.Data;
|
|
|
|
|
|
|
|
|
|
public class DashboardDbContext : DbContext
|
|
|
|
|
{
|
|
|
|
|
public DashboardDbContext(DbContextOptions<DashboardDbContext> options) : base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DbSet<Device> Devices { get; set; }
|
|
|
|
|
public DbSet<DeviceGroup> DeviceGroups { get; set; }
|
|
|
|
|
public DbSet<Application> Applications { get; set; }
|
|
|
|
|
public DbSet<DeviceAssignment> DeviceAssignments { get; set; }
|
|
|
|
|
public DbSet<User> Users { get; set; }
|
|
|
|
|
public DbSet<AuditLog> AuditLogs { get; set; }
|
|
|
|
|
public DbSet<Playlist> Playlists { get; set; }
|
|
|
|
|
public DbSet<PlaylistItem> PlaylistItems { get; set; }
|
|
|
|
|
public DbSet<AppVersion> AppVersions { get; set; }
|
2026-01-13 17:57:48 +08:00
|
|
|
public DbSet<RotatorItem> RotatorItems { get; set; }
|
2026-01-13 13:50:27 +08:00
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
|
|
|
|
|
// DeviceGroup 配置
|
|
|
|
|
modelBuilder.Entity<DeviceGroup>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasIndex(e => e.Name).IsUnique();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Device 配置
|
|
|
|
|
modelBuilder.Entity<Device>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasIndex(e => e.DeviceCode).IsUnique();
|
|
|
|
|
entity.HasOne(e => e.DeviceGroup)
|
|
|
|
|
.WithMany(g => g.Devices)
|
|
|
|
|
.HasForeignKey(e => e.DeviceGroupId)
|
|
|
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Application 配置
|
|
|
|
|
modelBuilder.Entity<Application>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasIndex(e => e.Name);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// DeviceAssignment 配置(多对多关系的中间表)
|
|
|
|
|
modelBuilder.Entity<DeviceAssignment>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasOne(e => e.Device)
|
|
|
|
|
.WithMany(d => d.Assignments)
|
|
|
|
|
.HasForeignKey(e => e.DeviceId)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
|
|
entity.HasOne(e => e.Application)
|
|
|
|
|
.WithMany(a => a.Assignments)
|
|
|
|
|
.HasForeignKey(e => e.ApplicationId)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
|
|
// 确保同一设备的同一应用只有一个分配
|
|
|
|
|
entity.HasIndex(e => new { e.DeviceId, e.ApplicationId }).IsUnique();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// User 配置
|
|
|
|
|
modelBuilder.Entity<User>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasIndex(e => e.Username).IsUnique();
|
|
|
|
|
entity.HasIndex(e => e.Email).IsUnique();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Playlist 配置
|
|
|
|
|
modelBuilder.Entity<Playlist>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasIndex(e => e.Name);
|
|
|
|
|
entity.HasOne(e => e.DeviceGroup)
|
|
|
|
|
.WithMany()
|
|
|
|
|
.HasForeignKey(e => e.DeviceGroupId)
|
|
|
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// PlaylistItem 配置
|
|
|
|
|
modelBuilder.Entity<PlaylistItem>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasOne(e => e.Playlist)
|
|
|
|
|
.WithMany(p => p.Items)
|
|
|
|
|
.HasForeignKey(e => e.PlaylistId)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
|
|
entity.HasOne(e => e.Application)
|
|
|
|
|
.WithMany()
|
|
|
|
|
.HasForeignKey(e => e.ApplicationId)
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
|
|
// 同一播放列表中的项目按 Order 排序
|
|
|
|
|
entity.HasIndex(e => new { e.PlaylistId, e.Order });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// AppVersion 配置
|
|
|
|
|
modelBuilder.Entity<AppVersion>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasIndex(e => new { e.Platform, e.Version });
|
|
|
|
|
});
|
2026-01-13 17:57:48 +08:00
|
|
|
|
|
|
|
|
// RotatorItem 配置
|
|
|
|
|
modelBuilder.Entity<RotatorItem>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasKey(e => e.Id);
|
|
|
|
|
|
|
|
|
|
// 不配置关系,只配置索引,避免 EF Core 创建影子属性
|
|
|
|
|
// 外键约束在迁移中已定义,这里不再重复配置
|
|
|
|
|
|
|
|
|
|
// 同一应用中的轮播项按 Order 排序
|
|
|
|
|
entity.HasIndex(e => new { e.ApplicationId, e.Order });
|
|
|
|
|
});
|
2026-01-13 13:50:27 +08:00
|
|
|
}
|
|
|
|
|
}
|