增加SqlSugar启动

This commit is contained in:
yubaolee
2023-08-26 23:52:30 +08:00
parent 0699413f39
commit a6c79bea7c
6 changed files with 615 additions and 312 deletions

View File

@@ -15,6 +15,7 @@ using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using OpenAuth.Repository.Interface;
using SqlSugar;
namespace OpenAuth.Repository.Test
{
@@ -65,6 +66,58 @@ namespace OpenAuth.Repository.Test
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
serviceCollection.AddDbContext<OpenAuthDBContext>();
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
.ToDictionary(x => x.Key, x => x.Value);
var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"];
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}");
serviceCollection.AddScoped<ISqlSugarClient>(s =>
{
SqlSugarClient sqlSugar;
if(dbtypes.ContainsValue(Define.DBTYPE_SQLSERVER))
{
sqlSugar = new SqlSugarClient (new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
ConnectionString = connectionString,
IsAutoCloseConnection = true,
});
}
else if(dbtypes.ContainsValue(Define.DBTYPE_MYSQL)) //mysql
{
sqlSugar = new SqlSugarClient (new ConnectionConfig()
{
DbType = SqlSugar.DbType.MySql,
ConnectionString = connectionString,
IsAutoCloseConnection = true,
});
}
else if(dbtypes.ContainsValue(Define.DBTYPE_PostgreSQL)) //PostgreSQL
{
sqlSugar = new SqlSugarClient (new ConnectionConfig()
{
DbType = SqlSugar.DbType.PostgreSQL,
ConnectionString = connectionString,
IsAutoCloseConnection = true,
});
}
else
{
sqlSugar = new SqlSugarClient (new ConnectionConfig()
{
DbType = SqlSugar.DbType.Oracle,
ConnectionString = connectionString,
IsAutoCloseConnection = true,
});
}
return sqlSugar;
});
var builder = new ContainerBuilder();
@@ -75,12 +128,6 @@ namespace OpenAuth.Repository.Test
var _container = builder.Build();
_autofacServiceProvider = new AutofacServiceProvider(_container);
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
.ToDictionary(x => x.Key, x => x.Value);
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
}