123 lines
4.1 KiB
C#
123 lines
4.1 KiB
C#
using Bunny.Common.Interceptor;
|
||
using Bunny.Dao.Entity.Base;
|
||
using Bunny.Dao.Entity.System;
|
||
using Microsoft.EntityFrameworkCore;
|
||
|
||
namespace Bunny.Common.Context;
|
||
|
||
/// <summary>
|
||
/// EfCore 连接内容
|
||
/// dotnet tool install --global dotnet-ef
|
||
/// dotnet add package Microsoft.EntityFrameworkCore.Design
|
||
/// dotnet ef migrations add InitialCreate
|
||
/// dotnet ef database update
|
||
/// </summary>
|
||
public class EfCoreContext : DbContext
|
||
{
|
||
public EfCoreContext()
|
||
{
|
||
var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
|
||
// const string dataBaseConnection =
|
||
// "192.168.3.98;Initial Catalog=BunnyDemo;TrustServerCertificate=True;Persist Security Info=True;User ID=sa;Password=abc1234.";
|
||
DbPath = $"Data Source={dataBaseConnection}";
|
||
}
|
||
|
||
// 连接路径
|
||
private string DbPath { get; }
|
||
public DbSet<Blog> Blogs { get; set; }
|
||
public DbSet<Post> Posts { get; set; }
|
||
public DbSet<Product> Products { get; set; }
|
||
public DbSet<User> Users { get; set; }
|
||
|
||
/// <summary>
|
||
/// 配置数据库上下文选项,指定数据库连接字符串
|
||
/// </summary>
|
||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||
{
|
||
options.UseSqlServer(DbPath).AddInterceptors(new TransactionInterceptor());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在创建模型时,为所有继承自BaseEntity的实体类型添加软删除查询过滤器
|
||
/// </summary>
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||
{
|
||
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
||
if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
|
||
entityType.AddSoftDeleteQueryFilter();
|
||
|
||
base.OnModelCreating(modelBuilder);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重写SaveChanges方法,在保存更改之前设置系统字段
|
||
/// </summary>
|
||
public override int SaveChanges()
|
||
{
|
||
SetSystemField();
|
||
return base.SaveChanges();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重写SaveChanges方法,在保存更改之前设置系统字段
|
||
/// </summary>
|
||
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
||
{
|
||
SetSystemField();
|
||
return base.SaveChanges(acceptAllChangesOnSuccess);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步保存更改,设置系统字段
|
||
/// </summary>
|
||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new())
|
||
{
|
||
SetSystemField();
|
||
return base.SaveChangesAsync(cancellationToken);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步保存更改,设置系统字段
|
||
/// </summary>
|
||
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
|
||
CancellationToken cancellationToken = new())
|
||
{
|
||
SetSystemField();
|
||
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 系统字段赋值
|
||
/// </summary>
|
||
private void SetSystemField()
|
||
{
|
||
foreach (var item in ChangeTracker.Entries())
|
||
if (item.Entity is BaseEntity entity)
|
||
{
|
||
var userId = BaseContext.GetUserId();
|
||
switch (item.State)
|
||
{
|
||
// 添加操作
|
||
case EntityState.Added:
|
||
{
|
||
if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid();
|
||
entity.CreateTime = DateTime.Now;
|
||
entity.UpdateTime = DateTime.Now;
|
||
// 判断用户Id是否为空
|
||
if (userId != null)
|
||
{
|
||
entity.CreateUserId = userId!.Value;
|
||
entity.UpdateUserId = userId!.Value;
|
||
}
|
||
|
||
break;
|
||
}
|
||
// 修改操作
|
||
case EntityState.Modified:
|
||
entity.UpdateTime = DateTime.Now;
|
||
if (userId != null) entity.UpdateUserId = userId!.Value;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
} |