FileShare/FileShare-EFCore/Database/AppDataContext.cs
lq1405 c6b05c12e5 fix(web): 恢复视频继续播放和进度保存
- 将播放器收敛为单个共享实例,避免列表/网格内多个 video ref 导致进度保存失效
- 恢复视频播放器上方的继续播放提示
- 在播放、暂停、拖动、结束、切换页面和离开页面时保存播放位置
- 保留文件浏览分页和排序参数的前端调用
2026-05-23 11:03:51 +08:00

108 lines
5.4 KiB
C#

using FileShare_EFCore.Models;
using Microsoft.EntityFrameworkCore;
namespace FileShare_EFCore.Database
{
/// <summary>
/// 应用数据库上下文 —— 继承自 FileShare-EFCore 的 AppDbContext。
/// 所有业务实体在此注册 DbSet。
/// 这是 FileShare-API 和 FileShare-PC 共用的具体数据上下文。
/// </summary>
public class AppDataContext(DatabaseConfiguration dbConfig) : AppDbContext(dbConfig)
{
/// <summary>天气预报数据</summary>
public DbSet<WeatherForecastEntity> WeatherForecasts => Set<WeatherForecastEntity>();
/// <summary>用户数据</summary>
public DbSet<UserEntity> Users => Set<UserEntity>();
/// <summary>API refresh token 数据</summary>
public DbSet<ApiRefreshTokenEntity> ApiRefreshTokens => Set<ApiRefreshTokenEntity>();
/// <summary>文件库根目录数据</summary>
public DbSet<ManagedLibraryRoot> ManagedLibraryRoots => Set<ManagedLibraryRoot>();
/// <summary>文件库文件记录数据</summary>
public DbSet<ManagedFileRecord> ManagedFileRecords => Set<ManagedFileRecord>();
/// <summary>文件缩略图映射数据</summary>
public DbSet<ManagedThumbnailMap> ManagedThumbnailMaps => Set<ManagedThumbnailMap>();
/// <summary>
/// 配置实体映射,包括主键、索引和属性约束。
/// </summary>
/// <param name="modelBuilder">模型构建器。</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<WeatherForecastEntity>(entity =>
{
entity.HasKey(e => e.Id).HasName("pk-weather-forecast");
entity.Property(e => e.Summary).HasMaxLength(200);
});
modelBuilder.Entity<UserEntity>(entity =>
{
entity.HasKey(e => e.Id).HasName("pk-user");
entity.Property(e => e.Email).HasMaxLength(200);
});
modelBuilder.Entity<ApiRefreshTokenEntity>(entity =>
{
entity.HasKey(e => e.Id).HasName("pk-api-refresh-token");
entity.HasIndex(e => e.TokenHash).IsUnique().HasDatabaseName("idx-api-refresh-token-hash");
entity.HasIndex(e => e.UserId).HasDatabaseName("idx-api-refresh-token-user-id");
});
modelBuilder.Entity<ManagedLibraryRoot>(entity =>
{
entity.HasKey(e => e.Id).HasName("pk-managed-library-root");
entity.HasIndex(e => e.Path).IsUnique().HasDatabaseName("idx-managed-library-root-path");
entity.Property(e => e.Path).HasMaxLength(1024);
entity.Property(e => e.DisplayName).HasMaxLength(200);
entity.Property(e => e.LastScanError).HasMaxLength(2000);
entity.Property(e => e.IsAvailable).HasDefaultValue(true);
});
modelBuilder.Entity<ManagedFileRecord>(entity =>
{
entity.HasKey(e => e.Id).HasName("pk-managed-file-record");
entity.HasIndex(e => e.LibraryRootId).HasDatabaseName("idx-managed-file-record-root-id");
entity.HasIndex(e => e.ThumbnailId).HasDatabaseName("idx-managed-file-record-thumbnail-id");
entity.HasIndex(e => e.AbsolutePath).IsUnique().HasDatabaseName("idx-managed-file-record-absolute-path");
entity.HasIndex(e => new { e.MediaType, e.Exists }).HasDatabaseName("idx-managed-file-record-media-type-exists");
entity.HasIndex(e => e.LastPlayedAt).HasDatabaseName("idx-managed-file-record-last-played-at");
entity.HasIndex(e => e.FileCreationTimeUtc).HasDatabaseName("idx-managed-file-record-file-creation-time");
entity.Property(e => e.FileName).HasMaxLength(260);
entity.Property(e => e.RelativePath).HasMaxLength(1024);
entity.Property(e => e.AbsolutePath).HasMaxLength(2048);
entity.Property(e => e.Extension).HasMaxLength(32);
entity.Property(e => e.MediaType).HasMaxLength(20);
entity.Property(e => e.ContentType).HasMaxLength(100);
entity.HasOne(e => e.LibraryRoot)
.WithMany(e => e.Files)
.HasForeignKey(e => e.LibraryRootId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(e => e.Thumbnail)
.WithMany(e => e.Files)
.HasForeignKey(e => e.ThumbnailId)
.OnDelete(DeleteBehavior.SetNull);
});
modelBuilder.Entity<ManagedThumbnailMap>(entity =>
{
entity.HasKey(e => e.Id).HasName("pk-managed-thumbnail-map");
entity.HasIndex(e => e.LibraryRootId).HasDatabaseName("idx-managed-thumbnail-map-root-id");
entity.HasIndex(e => e.RelativePath).IsUnique().HasDatabaseName("idx-managed-thumbnail-map-relative-path");
entity.Property(e => e.RelativePath).HasMaxLength(1024);
entity.Property(e => e.ContentType).HasMaxLength(100);
entity.HasOne(e => e.LibraryRoot)
.WithMany(e => e.Thumbnails)
.HasForeignKey(e => e.LibraryRootId)
.OnDelete(DeleteBehavior.Cascade);
});
}
}
}