You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7 KiB

3 months ago
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using CellularManagement.Domain.Entities;
using CellularManagement.Domain.Entities.Logging;
using CellularManagement.Domain.Entities.Device;
3 months ago
namespace CellularManagement.Infrastructure.Context;
/// <summary>
/// 应用程序数据库上下文
/// 继承自 IdentityDbContext 以支持身份认证
/// </summary>
public class AppDbContext : IdentityDbContext<AppUser, AppRole, string>
{
private readonly ILogger<AppDbContext> _logger;
/// <summary>
/// 用户角色关系
/// </summary>
public new DbSet<UserRole> UserRoles { get; set; } = null!;
/// <summary>
/// 权限集合
/// </summary>
public DbSet<Permission> Permissions { get; set; } = null!;
/// <summary>
/// 角色权限关联集合
/// </summary>
public DbSet<RolePermission> RolePermissions { get; set; } = null!;
/// <summary>
/// 用户登录日志
/// </summary>
public DbSet<LoginLog> LoginLogs { get; set; } = null!;
2 months ago
/// <summary>
/// 蜂窝设备集合
2 months ago
/// </summary>
public DbSet<CellularDevice> CellularDevices { get; set; } = null!;
/// <summary>
/// 协议版本集合
/// </summary>
public DbSet<ProtocolVersion> ProtocolVersions { get; set; } = null!;
2 months ago
/// <summary>
/// 网络配置集合
/// </summary>
public DbSet<NetworkConfig> NetworkConfigs { get; set; } = null!;
/// <summary>
/// 场景集合
/// </summary>
public DbSet<Scenario> Scenarios { get; set; } = null!;
3 months ago
/// <summary>
/// 初始化数据库上下文
/// </summary>
/// <param name="options">数据库上下文选项</param>
/// <param name="logger">日志记录器</param>
public AppDbContext(DbContextOptions<AppDbContext> options, ILogger<AppDbContext> logger)
: base(options)
{
_logger = logger;
}
/// <summary>
/// 配置实体模型
/// </summary>
/// <param name="modelBuilder">模型构建器</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// 应用所有的实体配置
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
// 忽略其他 Identity 相关的实体
3 months ago
modelBuilder.Ignore<IdentityUserLogin<string>>();
modelBuilder.Ignore<IdentityRoleClaim<string>>();
modelBuilder.Ignore<IdentityUserClaim<string>>();
modelBuilder.Ignore<IdentityUserToken<string>>();
modelBuilder.Ignore<IdentityUserRole<string>>();
}
}