1
This commit is contained in:
72
iMES.Core/EFDbContext/BaseDbContext.cs
Normal file
72
iMES.Core/EFDbContext/BaseDbContext.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using System.Text;
|
||||
using iMES.Core.Extensions;
|
||||
|
||||
namespace iMES.Core.EFDbContext
|
||||
{
|
||||
public abstract class BaseDbContext : DbContext
|
||||
{
|
||||
protected abstract string ConnectionString { get; }
|
||||
|
||||
public bool QueryTracking
|
||||
{
|
||||
set
|
||||
{
|
||||
this.ChangeTracker.QueryTrackingBehavior = value ? QueryTrackingBehavior.TrackAll : QueryTrackingBehavior.NoTracking;
|
||||
}
|
||||
}
|
||||
public BaseDbContext() : base() { }
|
||||
public BaseDbContext(DbContextOptions<BaseDbContext> options) : base(options) { }
|
||||
|
||||
protected void UseDbType(DbContextOptionsBuilder optionsBuilder,string connectionString)
|
||||
{
|
||||
if (Const.DBType.Name == Enums.DbCurrentType.MySql.ToString())
|
||||
{
|
||||
optionsBuilder.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 11)));
|
||||
}
|
||||
else if (Const.DBType.Name == Enums.DbCurrentType.PgSql.ToString())
|
||||
{
|
||||
optionsBuilder.UseNpgsql(connectionString);
|
||||
}
|
||||
else
|
||||
{
|
||||
optionsBuilder.UseSqlServer(connectionString);
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnModelCreating(ModelBuilder modelBuilder, Type type)
|
||||
{
|
||||
try
|
||||
{
|
||||
//获取所有类库
|
||||
var compilationLibrary = DependencyContext
|
||||
.Default
|
||||
.RuntimeLibraries
|
||||
.Where(x => !x.Serviceable && x.Type != "package" && x.Type == "project");
|
||||
foreach (var _compilation in compilationLibrary)
|
||||
{
|
||||
//加载指定类
|
||||
AssemblyLoadContext.Default
|
||||
.LoadFromAssemblyName(new AssemblyName(_compilation.Name))
|
||||
.GetTypes().Where(x => x.GetTypeInfo().BaseType != null
|
||||
&& x.BaseType == (type)).ToList()
|
||||
.ForEach(t => { modelBuilder.Entity(t); });
|
||||
}
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string mapPath = ($"Log/").MapPath();
|
||||
Utilities.FileHelper.WriteFile(mapPath, $"syslog_{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt", ex.Message + ex.StackTrace + ex.Source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
34
iMES.Core/EFDbContext/EFLoggerProvider.cs
Normal file
34
iMES.Core/EFDbContext/EFLoggerProvider.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using iMES.Core.Utilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace iMES.Core.EFDbContext
|
||||
{
|
||||
public class EFLoggerProvider : ILoggerProvider
|
||||
{
|
||||
public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName);
|
||||
public void Dispose() { }
|
||||
}
|
||||
public class EFLogger : ILogger
|
||||
{
|
||||
private readonly string categoryName;
|
||||
|
||||
public EFLogger(string categoryName) => this.categoryName = categoryName;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
//ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
|
||||
if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
|
||||
&& logLevel == LogLevel.Information)
|
||||
{
|
||||
var logContent = formatter(state, exception);
|
||||
Console.WriteLine(logContent);
|
||||
}
|
||||
}
|
||||
public IDisposable BeginScope<TState>(TState state) => null;
|
||||
}
|
||||
}
|
||||
36
iMES.Core/EFDbContext/ReportDbContext.cs
Normal file
36
iMES.Core/EFDbContext/ReportDbContext.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using iMES.Core.DBManager;
|
||||
using iMES.Core.Extensions.AutofacManager;
|
||||
using iMES.Entity.SystemModels;
|
||||
|
||||
namespace iMES.Core.EFDbContext
|
||||
{
|
||||
public class ReportDbContext : BaseDbContext, IDependency
|
||||
{
|
||||
protected override string ConnectionString
|
||||
{
|
||||
get
|
||||
{
|
||||
return DBServerProvider.ReportConnectingString;
|
||||
}
|
||||
}
|
||||
public ReportDbContext() : base() { }
|
||||
|
||||
public ReportDbContext(DbContextOptions<BaseDbContext> options) : base(options) { }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.UseDbType(optionsBuilder, ConnectionString);
|
||||
//默认禁用实体跟踪
|
||||
optionsBuilder = optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder, typeof(ReportEntity));
|
||||
}
|
||||
}
|
||||
}
|
||||
36
iMES.Core/EFDbContext/ServiceDbContext.cs
Normal file
36
iMES.Core/EFDbContext/ServiceDbContext.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using iMES.Core.DBManager;
|
||||
using iMES.Core.Extensions.AutofacManager;
|
||||
using iMES.Entity.SystemModels;
|
||||
|
||||
namespace iMES.Core.EFDbContext
|
||||
{
|
||||
public class ServiceDbContext : BaseDbContext, IDependency
|
||||
{
|
||||
protected override string ConnectionString
|
||||
{
|
||||
get
|
||||
{
|
||||
return DBServerProvider.ServiceUserCurrnetConnectingString;
|
||||
}
|
||||
}
|
||||
public ServiceDbContext() : base() { }
|
||||
|
||||
public ServiceDbContext(DbContextOptions<BaseDbContext> options) : base(options) { }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.UseDbType(optionsBuilder, ConnectionString);
|
||||
//默认禁用实体跟踪
|
||||
optionsBuilder = optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder, typeof(ServiceEntity));
|
||||
}
|
||||
}
|
||||
}
|
||||
36
iMES.Core/EFDbContext/SysDbContext.cs
Normal file
36
iMES.Core/EFDbContext/SysDbContext.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using iMES.Core.DBManager;
|
||||
using iMES.Core.Extensions.AutofacManager;
|
||||
using iMES.Entity.SystemModels;
|
||||
|
||||
namespace iMES.Core.EFDbContext
|
||||
{
|
||||
public class SysDbContext : BaseDbContext, IDependency
|
||||
{
|
||||
protected override string ConnectionString
|
||||
{
|
||||
get
|
||||
{
|
||||
return DBServerProvider.SysConnectingString;
|
||||
}
|
||||
}
|
||||
public SysDbContext() : base() { }
|
||||
|
||||
public SysDbContext(DbContextOptions<BaseDbContext> options) : base(options) { }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.UseDbType(optionsBuilder, ConnectionString);
|
||||
//默认禁用实体跟踪
|
||||
optionsBuilder = optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder, typeof(SysEntity));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user