77 lines
3.3 KiB
C#
77 lines
3.3 KiB
C#
using System.Linq.Expressions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MilliSim.Common.Entities;
|
|
|
|
namespace MilliSim.Data;
|
|
|
|
public class MilliSimDbContext : DbContext
|
|
{
|
|
public MilliSimDbContext(DbContextOptions<MilliSimDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<User> Users => Set<User>();
|
|
public DbSet<Project> Projects => Set<Project>();
|
|
public DbSet<ProjectCategory> ProjectCategories => Set<ProjectCategory>();
|
|
public DbSet<PlatformType> PlatformTypes => Set<PlatformType>();
|
|
public DbSet<FeatureTag> FeatureTags => Set<FeatureTag>();
|
|
public DbSet<ProjectTagRelation> ProjectTagRelations => Set<ProjectTagRelation>();
|
|
public DbSet<ProjectScreenshot> ProjectScreenshots => Set<ProjectScreenshot>();
|
|
public DbSet<ProjectPackage> ProjectPackages => Set<ProjectPackage>();
|
|
public DbSet<Favorite> Favorites => Set<Favorite>();
|
|
public DbSet<ExperienceLog> ExperienceLogs => Set<ExperienceLog>();
|
|
public DbSet<Authorization> Authorizations => Set<Authorization>();
|
|
public DbSet<Banner> Banners => Set<Banner>();
|
|
public DbSet<Partner> Partners => Set<Partner>();
|
|
public DbSet<Consultation> Consultations => Set<Consultation>();
|
|
public DbSet<Notification> Notifications => Set<Notification>();
|
|
public DbSet<NotificationRead> NotificationReads => Set<NotificationRead>();
|
|
public DbSet<OperationLog> OperationLogs => Set<OperationLog>();
|
|
public DbSet<SystemSetting> SystemSettings => Set<SystemSetting>();
|
|
public DbSet<Backup> Backups => Set<Backup>();
|
|
public DbSet<WakeProtocol> WakeProtocols => Set<WakeProtocol>();
|
|
public DbSet<PendingUpload> PendingUploads => Set<PendingUpload>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
// 软删除全局过滤器
|
|
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
|
{
|
|
if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
|
|
{
|
|
var parameter = Expression.Parameter(entityType.ClrType, "e");
|
|
var property = Expression.Property(parameter, nameof(BaseEntity.IsDeleted));
|
|
var falseValue = Expression.Constant(false);
|
|
var comparison = Expression.Equal(property, falseValue);
|
|
var lambda = Expression.Lambda(comparison, parameter);
|
|
modelBuilder.Entity(entityType.ClrType).HasQueryFilter((dynamic)lambda);
|
|
}
|
|
}
|
|
|
|
// 唯一索引
|
|
modelBuilder.Entity<User>().HasIndex(u => u.Username).IsUnique();
|
|
modelBuilder.Entity<SystemSetting>().HasIndex(s => s.Key).IsUnique();
|
|
|
|
// PendingUpload 索引(用于垃圾资源清理查询)
|
|
modelBuilder.Entity<PendingUpload>().HasIndex(p => p.ObjectKey);
|
|
modelBuilder.Entity<PendingUpload>().HasIndex(p => p.ConsumedAt);
|
|
|
|
// 字符集
|
|
modelBuilder.Entity<Project>().Property(p => p.Content).HasColumnType("longtext");
|
|
|
|
// 精度
|
|
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
|
{
|
|
foreach (var property in entityType.GetProperties())
|
|
{
|
|
if (property.ClrType == typeof(DateTime))
|
|
{
|
|
property.SetColumnType("datetime");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|