@@ -25,8 +25,10 @@ namespace HealthMonitor.Core.Dal.Factory | |||
public virtual IGpsCardDataAccessor GetDbContext() | |||
{ | |||
var options = new DbContextOptionsBuilder<GpsCardContext>() | |||
.UseMySql(_configuration.GetConnectionString("GpsCard_Connection_String")) | |||
var mySqlCon = _configuration.GetConnectionString("GpsCard_Connection_String"); | |||
var serverVersion = ServerVersion.AutoDetect(mySqlCon); | |||
var options = new DbContextOptionsBuilder<GpsCardContext>() | |||
.UseMySql(mySqlCon,serverVersion) | |||
.UseLoggerFactory(_loggerFactory) | |||
.Options; | |||
@@ -25,12 +25,19 @@ namespace HealthMonitor.Core.Dal.Factory | |||
public virtual IHealthMonitorDataAccessor GetDbContext() | |||
{ | |||
var options = new DbContextOptionsBuilder<HealthMonitorContext>() | |||
.UseMySql(_configuration.GetConnectionString("HealthMonitor_Connection_String")) | |||
.UseLoggerFactory(_loggerFactory) | |||
.Options; | |||
//var options = new DbContextOptionsBuilder<HealthMonitorContext>() | |||
// .UseMySql(_configuration.GetConnectionString("HealthMonitor_Connection_String")) | |||
// .UseLoggerFactory(_loggerFactory) | |||
// .Options; | |||
var context = new HealthMonitorContext(options); | |||
var mySqlCon = _configuration.GetConnectionString("HealthMonitor_Connection_String"); | |||
var serverVersion = ServerVersion.AutoDetect(mySqlCon); | |||
var options = new DbContextOptionsBuilder<HealthMonitorContext>() | |||
.UseMySql(mySqlCon, serverVersion) | |||
.UseLoggerFactory(_loggerFactory) | |||
.Options; | |||
var context = new HealthMonitorContext(options); | |||
return new EfCoreDataAccessor(context); | |||
////AOP需要依赖DI | |||
@@ -0,0 +1,48 @@ | |||
using Microsoft.EntityFrameworkCore; | |||
using Microsoft.Extensions.Configuration; | |||
using Microsoft.Extensions.Logging; | |||
using HealthMonitor.Core.Context; | |||
using HealthMonitor.Core.DbLog; | |||
using HealthMonitor.Core.Dal.EfCoreImpl; | |||
namespace HealthMonitor.Core.Dal.Factory | |||
{ | |||
/// <summary> | |||
/// 瞬态HealthMonitorContext构建帮助类 | |||
/// </summary> | |||
public class HealthMonitorTDengineAccessorFactory | |||
{ | |||
private readonly IConfiguration _configuration; | |||
private readonly ILogger<HealthMonitorTDengineAccessorFactory> _logger; | |||
private readonly ILoggerFactory _loggerFactory; | |||
public HealthMonitorTDengineAccessorFactory(IConfiguration configuration, ILogger<HealthMonitorTDengineAccessorFactory> logger, IEfCoreLoggerProvider loggerProvider) | |||
{ | |||
_configuration = configuration; | |||
_logger = logger; | |||
_loggerFactory = new LoggerFactory(new[] { loggerProvider }); | |||
} | |||
//public virtual IHealthMonitorDataAccessor GetDbContext() | |||
//{ | |||
// //var options = new DbContextOptionsBuilder<HealthMonitorContext>() | |||
// // .UseMySql(_configuration.GetConnectionString("HealthMonitor_Connection_String")) | |||
// // .UseLoggerFactory(_loggerFactory) | |||
// // .Options; | |||
// //var context = new HealthMonitorContext(options); | |||
// //return new EfCoreDataAccessor(context); | |||
// ////AOP需要依赖DI | |||
// //var services = new ServiceCollection(); | |||
// //services.AddTransient<IGpsCardDataAccessor, EfCoreDataAccessor>(sp => | |||
// //{ | |||
// // var context = new GpsCardContext(options); | |||
// // return new EfCoreDataAccessor(context); | |||
// //}); | |||
// //var provider = services.BuildDynamicProxyProvider(); | |||
// //return provider.GetRequiredService<IGpsCardDataAccessor>(); | |||
//} | |||
} | |||
} |
@@ -9,7 +9,7 @@ | |||
<ItemGroup> | |||
<PackageReference Include="AspectCore.Extensions.DependencyInjection" Version="2.3.0" /> | |||
<PackageReference Include="CSRedisCore" Version="3.6.8" /> | |||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.7" /> | |||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
@@ -58,13 +58,18 @@ namespace HealthMonitor.WebApi | |||
.AddDbContextPool<GpsCardContext>((sp, options) => | |||
{ | |||
var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>(); | |||
options.UseMySql(builder.Configuration.GetConnectionString("GpsCard_Connection_String")) | |||
var mySqlCon = builder.Configuration.GetConnectionString("GpsCard_Connection_String"); | |||
var serverVersion = ServerVersion.AutoDetect(mySqlCon); | |||
options.UseMySql(mySqlCon, serverVersion) | |||
.UseLoggerFactory(loggerFactory); | |||
}, poolSize: 64) // health_monitor | |||
.AddDbContextPool<HealthMonitorContext>((sp, options) => | |||
{ | |||
var loggerFactory = sp.GetRequiredService<EfCoreLoggerFactory>(); | |||
options.UseMySql(builder.Configuration.GetConnectionString("HealthMonitor_Connection_String")) | |||
var mySqlCon = builder.Configuration.GetConnectionString("HealthMonitor_Connection_String"); | |||
var serverVersion = ServerVersion.AutoDetect(mySqlCon); | |||
options.UseMySql(mySqlCon, serverVersion) | |||
.UseLoggerFactory(loggerFactory); | |||
}, poolSize: 64); ; | |||