using LMS.Repository.DB; using LMS.Repository.Models.DB; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System.Text.Json; namespace LMS.DAO { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } //public DbSet Prompt { get; set; } //public DbSet PromptType { get; set; } public DbSet Permission { get; set; } public DbSet PermissionType { get; set; } public DbSet PromptType { get; set; } public DbSet Prompt { get; set; } public DbSet Machine { get; set; } public DbSet RefreshTokens { get; set; } public DbSet ApiEndpoints { get; set; } public DbSet RsaKeys { get; set; } public DbSet Options { get; set; } public DbSet Software { get; set; } public DbSet UserSoftware { get; set; } public DbSet SoftwareControl { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .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() // 如果存储的是空字符串或空数组,则返回空列表 : JsonSerializer.Deserialize>(v, (JsonSerializerOptions?)null) ?? new List() ).Metadata.SetValueComparer( new ValueComparer>( // 比较两个集合是否相等 (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() ) ); modelBuilder.Entity() .HasKey(us => new { us.UserId, us.SoftwareId }); } } }