yubaolee
2021-04-21 18:02:01 +08:00
parent d4894b5ba3
commit b278a1ec10
19 changed files with 124 additions and 65 deletions

View File

@@ -499,11 +499,6 @@ namespace OpenAuth.App
FileHelper.RegxAddContentByParenthesis(openAuthDBContextPath, "public virtual DbSet<" + tableInfo.ClassName + "> " + tableInfo.TableName + "s { get; set; }");
}
private bool IsMysql()
{
return (_appConfiguration.Value.DbType == Define.DBTYPE_MYSQL);
}
Dictionary<string, Type> PrimitiveTypes = new Dictionary<string, Type>()
{
{"int", typeof(int)}

View File

@@ -5,6 +5,8 @@ using System.Data;
using System.Linq;
using Autofac.Extensions.DependencyInjection;
using Infrastructure;
using Infrastructure.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
@@ -22,10 +24,12 @@ namespace OpenAuth.App
private List<DbContext> _contexts = new List<DbContext>();
private IOptions<AppSetting> _appConfiguration;
private IHttpContextAccessor _httpContextAccessor;
public DbExtension(IOptions<AppSetting> appConfiguration, OpenAuthDBContext openAuthDbContext)
public DbExtension(IOptions<AppSetting> appConfiguration, OpenAuthDBContext openAuthDbContext, IHttpContextAccessor httpContextAccessor)
{
_appConfiguration = appConfiguration;
_httpContextAccessor = httpContextAccessor;
_contexts.Add(openAuthDbContext); //如果有多个DBContext可以按OpenAuthDBContext同样的方式添加到_contexts中
}
@@ -105,7 +109,8 @@ namespace OpenAuth.App
/// <returns></returns>
public IList<SysTableColumn> GetDbTableStructure(string tableName)
{
if (_appConfiguration.Value.DbType == Define.DBTYPE_MYSQL)
var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
if (dbtype == Define.DBTYPE_MYSQL)
{
return GetMySqlStructure(tableName);
}

View File

@@ -2,6 +2,8 @@ using System;
using System.Linq;
using System.Threading.Tasks;
using Infrastructure;
using Infrastructure.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
@@ -17,6 +19,7 @@ namespace OpenAuth.App
{
private IAuth _auth;
private IOptions<AppSetting> _appConfiguration;
private IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// 加载列表
/// </summary>
@@ -44,7 +47,9 @@ namespace OpenAuth.App
UnitWork.Add(obj);
if (!string.IsNullOrEmpty(obj.DbName))
{
UnitWork.ExecuteSql(FormUtil.GetSql(obj, _appConfiguration.Value.DbType));
var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
UnitWork.ExecuteSql(FormUtil.GetSql(obj, dbtype));
}
UnitWork.Save();
}
@@ -67,7 +72,8 @@ namespace OpenAuth.App
if (!string.IsNullOrEmpty(obj.DbName))
{
UnitWork.ExecuteSql(FormUtil.GetSql(obj, _appConfiguration.Value.DbType));
var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
UnitWork.ExecuteSql(FormUtil.GetSql(obj, dbtype));
}
}
@@ -78,10 +84,11 @@ namespace OpenAuth.App
}
public FormApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<Form,OpenAuthDBContext> repository,
IAuth auth, IOptions<AppSetting> appConfiguration) : base(unitWork, repository, auth)
IAuth auth, IOptions<AppSetting> appConfiguration, IHttpContextAccessor httpContextAccessor) : base(unitWork, repository, auth)
{
_auth = auth;
_appConfiguration = appConfiguration;
_httpContextAccessor = httpContextAccessor;
}
}
}

View File

@@ -1,8 +1,10 @@
using System;
using System.IO;
using System.Linq;
using Autofac.Extensions.DependencyInjection;
using Infrastructure;
using Infrastructure.Extensions.AutofacManager;
using Infrastructure.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -33,8 +35,7 @@ namespace OpenAuth.App.Test
.AddJsonFile("appsettings.Development.json", optional: true)
.AddEnvironmentVariables()
.Build();
Console.WriteLine($"单元测试数据库信息:{config.GetSection("AppSetting")["DbType"]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
serviceCollection.Configure<AppSetting>(config.GetSection("AppSetting"));
//添加log4net
serviceCollection.AddLogging(builder =>
@@ -57,6 +58,12 @@ namespace OpenAuth.App.Test
var container = AutofacExt.InitForTest(serviceCollection);
_autofacServiceProvider = new AutofacServiceProvider(container);
AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
.ToDictionary(x => x.Key, x => x.Value);
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
}
/// <summary>

View File

@@ -12,7 +12,6 @@ using Microsoft.Extensions.Hosting;
using Moq;
using OpenAuth.App.Request;
using OpenAuth.App.SSO;
using HttpContext = Infrastructure.Utilities.HttpContext;
namespace OpenAuth.App.Test
{