LMS.service/LMS.DAO/ApplicationDbContext.cs

118 lines
4.7 KiB
C#
Raw Normal View History

2024-10-13 17:04:47 +08:00

using LMS.Repository.DB;
using LMS.Repository.MJPackage;
2024-10-13 17:04:47 +08:00
using LMS.Repository.Models.DB;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
//using Newtonsoft.Json;
2024-10-13 17:04:47 +08:00
using System.Text.Json;
namespace LMS.DAO
{
public class ApplicationDbContext : IdentityDbContext<User, Role, long>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
//public DbSet<Prompt> Prompt { get; set; }
//public DbSet<PromptType> PromptType { get; set; }
public DbSet<Permission> Permission { get; set; }
public DbSet<PermissionType> PermissionType { get; set; }
public DbSet<PromptType> PromptType { get; set; }
public DbSet<Prompt> Prompt { get; set; }
2024-10-13 17:04:47 +08:00
public DbSet<Machine> Machine { get; set; }
public DbSet<RefreshTokens> RefreshTokens { get; set; }
public DbSet<ApiEndpoints> ApiEndpoints { get; set; }
public DbSet<RsaKeys> RsaKeys { get; set; }
2024-10-18 12:44:12 +08:00
public DbSet<Options> Options { get; set; }
public DbSet<Software> Software { get; set; }
public DbSet<UserSoftware> UserSoftware { get; set; }
public DbSet<SoftwareControl> SoftwareControl { get; set; }
public DbSet<MachineAuthorization> MachineAuthorization { get; set; }
public DbSet<DataInfo> DataInfo { get; set; }
public DbSet<MJApiTokens> MJApiTokens { get; set; }
public DbSet<MJApiTokenUsage> MJApiTokenUsage { get; set; }
public DbSet<MJApiTasks> MJApiTasks { get; set; }
2024-10-13 17:04:47 +08:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApiEndpoints>()
.Property(a => a.RequiredPermissionIds)
.HasConversion(
v => string.IsNullOrEmpty(JsonSerializer.Serialize(v, (JsonSerializerOptions?)null))
? "[]" // 如果序列化结果为空,则存储空数组
: JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v => string.IsNullOrEmpty(v) || v == "[]"
? new List<string>() // 如果存储的是空字符串或空数组,则返回空列表
: JsonSerializer.Deserialize<List<string>>(v, (JsonSerializerOptions?)null) ?? new List<string>()
).Metadata.SetValueComparer(
new ValueComparer<List<string>>(
// 比较两个集合是否相等
(c1, c2) => c1 != null && c2 != null && c1.SequenceEqual(c2),
// 计算集合的哈希码 - 这里修复了问题
c => c == null ? 0 : c.Aggregate(0, (a, v) => HashCode.Combine(a, v != null ? v.GetHashCode() : 0)),
// 创建集合的副本
c => c == null ? null : c.ToList()
)
2024-10-13 17:04:47 +08:00
);
modelBuilder.Entity<UserSoftware>()
.HasKey(us => new { us.UserId, us.SoftwareId });
// Options表的RoleIds字段使用JSON格式存储
modelBuilder.Entity<Options>(entity =>
{
entity.Property(e => e.RoleIds)
.HasConversion(
v => Newtonsoft.Json.JsonConvert.SerializeObject(v), // 将 List<int> 序列化为 JSON 字符串
v => Newtonsoft.Json.JsonConvert.DeserializeObject<List<long>>(v ?? "[]") ?? new List<long>() // 反序列化
)
.HasColumnType("json"); // 指定MySQL字段类型为JSON
});
modelBuilder.Entity<MJApiTokens>(entity =>
{
entity.ToTable("MJApiTokens");
entity.Property(e => e.Token).IsRequired().HasMaxLength(64);
entity.Property(e => e.DailyLimit).HasDefaultValue(0);
entity.Property(e => e.TotalLimit).HasDefaultValue(0);
entity.Property(e => e.ConcurrencyLimit).HasDefaultValue(1);
entity.Property(e => e.CreatedAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
entity.HasIndex(e => e.Token).IsUnique();
entity.HasIndex(e => e.ExpiresAt);
});
modelBuilder.Entity<MJApiTokenUsage>(entity =>
{
entity.ToTable("MJApiTokenUsage");
entity.HasKey(e => new { e.TokenId, e.Date });
entity.HasOne<MJApiTokens>()
.WithMany()
.HasForeignKey(e => e.TokenId)
.OnDelete(DeleteBehavior.Cascade);
});
2024-10-13 17:04:47 +08:00
}
}
}