initial
This commit is contained in:
105
src/DRS9.Dashboard.Infrastructure/Data/DashboardDbContext.cs
Normal file
105
src/DRS9.Dashboard.Infrastructure/Data/DashboardDbContext.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
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; }
|
||||
|
||||
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 });
|
||||
});
|
||||
}
|
||||
}
|
||||
44
src/DRS9.Dashboard.Infrastructure/Data/DataSeeder.cs
Normal file
44
src/DRS9.Dashboard.Infrastructure/Data/DataSeeder.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using DRS9.Dashboard.Domain.Entities;
|
||||
|
||||
namespace DRS9.Dashboard.Infrastructure.Data;
|
||||
|
||||
public static class DataSeeder
|
||||
{
|
||||
public static async Task SeedAsync(DashboardDbContext context)
|
||||
{
|
||||
// 确保数据库已创建
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
|
||||
// 添加测试设备码
|
||||
if (!context.Devices.Any())
|
||||
{
|
||||
var devices = new List<Device>
|
||||
{
|
||||
new() { DeviceCode = "TEST-001-DEV", DeviceName = "测试设备1", IsEnabled = true },
|
||||
new() { DeviceCode = "TEST-002-DEV", DeviceName = "测试设备2", IsEnabled = true },
|
||||
new() { DeviceCode = "TEST-003-DEV", DeviceName = "测试设备3", IsEnabled = true },
|
||||
new() { DeviceCode = "SCHOOL-001-DS", DeviceName = "学校显示屏1", IsEnabled = true },
|
||||
new() { DeviceCode = "MALL-001-DS", DeviceName = "商场广告屏1", IsEnabled = true }
|
||||
};
|
||||
|
||||
await context.Devices.AddRangeAsync(devices);
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine("已添加 5 个测试设备码");
|
||||
}
|
||||
|
||||
// 添加测试应用
|
||||
if (!context.Applications.Any())
|
||||
{
|
||||
var apps = new List<Application>
|
||||
{
|
||||
new() { Name = "欢迎页面", Type = "Dashboard", ContentUrl = "/dashboard/welcome", IsEnabled = true, Priority = 1 },
|
||||
new() { Name = "新闻轮转", Type = "WebRotator", ContentUrl = "/rotator/news", IsEnabled = true, Priority = 2 },
|
||||
new() { Name = "图片轮播", Type = "ImageRotator", ContentUrl = "/gallery/promo", IsEnabled = true, Priority = 3 }
|
||||
};
|
||||
|
||||
await context.Applications.AddRangeAsync(apps);
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine("已添加 3 个测试应用");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user