|
@@ -1,6 +1,10 @@
|
|
|
using ERP.Core.Entity;
|
|
|
+using ERP.Framework.WebApi;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using Serilog;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
@@ -18,12 +22,37 @@ namespace ERP.Core.Context
|
|
|
this._configuration = configuration;
|
|
|
}
|
|
|
|
|
|
+ public override int SaveChanges()
|
|
|
+ {
|
|
|
+ var entityEntries = ChangeTracker.Entries().ToList();
|
|
|
+ foreach(var entry in entityEntries)
|
|
|
+ {
|
|
|
+ if (entry.State == EntityState.Added)
|
|
|
+ {
|
|
|
+ Entry(entry.Entity).Property(nameof(BaseEntity.CreateTime)).CurrentValue = DateTime.Now;
|
|
|
+
|
|
|
+ //Entry(entry.Entity).Property(nameof(BaseEntity.CreateBy)).CurrentValue = DateTime.Now;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (entry.State == EntityState.Modified)
|
|
|
+ {
|
|
|
+ Entry(entry.Entity).Property(nameof(BaseEntity.UpdateTime)).CurrentValue = DateTime.Now;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return base.SaveChanges();
|
|
|
+ }
|
|
|
+
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
{
|
|
|
+ var name = GetType().Name;
|
|
|
// 可扩展为多租户,从当前请求头拿租户Id,根据租户Id获取对应数据库配置名,替换
|
|
|
- string connect = _configuration.GetConnectionString(GetType().Name)!;
|
|
|
-
|
|
|
+ string connect = _configuration.GetConnectionString(name)!;
|
|
|
+ // 可扩展为使用多种数据库,添加配置项数组根据不同租户Id,配置不同库类型,Key为租户Id/库名,对象包含数据库类型等
|
|
|
optionsBuilder.UseMySql(connect,new MySqlServerVersion(new Version(8,0,26)));
|
|
|
+
|
|
|
+ UseLog(optionsBuilder);
|
|
|
+
|
|
|
base.OnConfiguring(optionsBuilder);
|
|
|
}
|
|
|
|
|
@@ -33,10 +62,18 @@ namespace ERP.Core.Context
|
|
|
/// <param name="modelBuilder"></param>
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
{
|
|
|
+ }
|
|
|
|
|
|
+ private void UseLog(DbContextOptionsBuilder optionsBuilder)
|
|
|
+ {
|
|
|
+ optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder =>
|
|
|
+ {
|
|
|
+ builder.AddConsole();
|
|
|
+ builder.AddSerilog();
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
- public virtual DbSet<SysUser> SysUsers { get; set; }
|
|
|
+ public virtual DbSet<SysUser> SysUser { get; set; }
|
|
|
|
|
|
|
|
|
}
|