test mysql unit test and support single node read single node write

This commit is contained in:
xuejmnet 2021-08-22 18:21:39 +08:00
parent 3d0f1b12a0
commit 6af0fd8896
15 changed files with 61 additions and 26 deletions

2
.gitignore vendored
View File

@ -343,4 +343,4 @@ healthchecksdb
/src/Bd.ShopRent.Api/Properties/launchSettings.json /src/Bd.ShopRent.Api/Properties/launchSettings.json
/src/Bd.ShopRent.DTO/__autoDTOConfig.json /src/Bd.ShopRent.DTO/__autoDTOConfig.json
/src/Bd.ShopRent.DTO/Bd.ShopRent.Dto.csproj /src/Bd.ShopRent.DTO/Bd.ShopRent.Dto.csproj
/test/ShardingCore.Test50/Configs/* MacDbConfig.json

View File

@ -16,7 +16,7 @@ Release | EF Core | .NET Standard | .NET (Core)
### 数据库支持 ### 数据库支持
数据库 | 是否支持 | 支持情况 数据库 | 是否支持 | 支持情况
--- | --- | --- --- | --- | ---
SqlServer | 是 | 80%近乎完美 SqlServer | 是 | 90%近乎完美
MySql |支持 | 未测试 MySql |支持 | 未测试
PostgreSql | 支持 | 未测试 PostgreSql | 支持 | 未测试
SQLite | 支持 | 未测试 SQLite | 支持 | 未测试
@ -40,6 +40,7 @@ Oracle | 支持 | 未测试
- [自动建表](#自动建表) - [自动建表](#自动建表)
- [事务](#事务) - [事务](#事务)
- [批量操作](#批量操作) - [批量操作](#批量操作)
- [读写分离](#读写分离)
- [注意事项](#注意事项) - [注意事项](#注意事项)
- [计划(Future)](#计划) - [计划(Future)](#计划)
- [最后](#最后) - [最后](#最后)
@ -94,7 +95,11 @@ Oracle | 支持 | 未测试
## 安装 ## 安装
```xml ```xml
<PackageReference Include="ShardingCore" Version="5.2.0.03" /> <PackageReference Include="ShardingCore" Version="5.2.0.07" />
or
<PackageReference Include="ShardingCore" Version="3.2.0.07" />
or
<PackageReference Include="ShardingCore" Version="2.2.0.07" />
``` ```
## 配置 ## 配置
@ -154,7 +159,7 @@ Oracle | 支持 | 未测试
modelBuilder.ApplyConfiguration(new SysTestMap()); modelBuilder.ApplyConfiguration(new SysTestMap());
} }
public string ModelChangeKey { get; set; } public IRouteTail RouteTail { get; set; }
} }
``` ```
@ -196,7 +201,6 @@ Oracle | 支持 | 未测试
{ {
op.EnsureCreatedWithOutShardingTable = true; op.EnsureCreatedWithOutShardingTable = true;
op.CreateShardingTableOnStart = true; op.CreateShardingTableOnStart = true;
//不支持mars额外加一条字符串的
op.UseShardingOptionsBuilder( op.UseShardingOptionsBuilder(
(connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger), (connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),
builder => builder.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;").UseLoggerFactory(efLogger)); builder => builder.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;").UseLoggerFactory(efLogger));
@ -308,6 +312,22 @@ AbstractSimpleShardingYearKeyLongVirtualTableRoute |按时间戳 |yyyy | `>,>=,<
tran.commit() tran.commit()
``` ```
## 读写分离
该框架目前已经支持单node的读写分离,后续框架将支持多node的读
```c#
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
,op =>
{
op.EnsureCreatedWithOutShardingTable = true;
op.CreateShardingTableOnStart = true;
op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer("write db connection string").UseLoggerFactory(efLogger),
(conStr,builder)=> builder.UseSqlServer("read db connection string").UseLoggerFactory(efLogger));
op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
});
```
# 注意事项 # 注意事项
该库的追踪是基于adonet的MARS(MultipleActiveResultSets=True;)所以基本不支持该特性的无法支持完美追踪 该库的追踪是基于adonet的MARS(MultipleActiveResultSets=True;)所以基本不支持该特性的无法支持完美追踪

View File

@ -31,7 +31,7 @@ namespace Sample.SqlServer
op.EnsureCreatedWithOutShardingTable = true; op.EnsureCreatedWithOutShardingTable = true;
op.CreateShardingTableOnStart = true; op.CreateShardingTableOnStart = true;
op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger), op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),
builder => builder.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;").UseLoggerFactory(efLogger)); (conStr,builder) => builder.UseSqlServer(conStr).UseLoggerFactory(efLogger));
op.AddShardingTableRoute<SysUserModVirtualTableRoute>(); op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
}); });
////不支持MARS不支持追踪的 ////不支持MARS不支持追踪的

View File

@ -65,7 +65,8 @@ namespace Samples.AbpSharding
private DbContextOptions<T> CreateMonopolyDbContextOptions() private DbContextOptions<T> CreateMonopolyDbContextOptions()
{ {
var dbContextOptionBuilder = CreateDbContextOptionBuilder(); var dbContextOptionBuilder = CreateDbContextOptionBuilder();
_shardingDbContextOptionsBuilderConfig.UseDbContextOptionsBuilder(dbContextOptionBuilder); var connectionString = Database.GetConnectionString();
_shardingDbContextOptionsBuilderConfig.UseDbContextOptionsBuilder(connectionString,dbContextOptionBuilder);
return dbContextOptionBuilder.Options; return dbContextOptionBuilder.Options;
} }

View File

@ -17,6 +17,6 @@ namespace ShardingCore
{ {
Type ShardingDbContextType { get; } Type ShardingDbContextType { get; }
DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder); DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder);
DbContextOptionsBuilder UseDbContextOptionsBuilder(DbContextOptionsBuilder dbContextOptionsBuilder); DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString,DbContextOptionsBuilder dbContextOptionsBuilder);
} }
} }

View File

@ -77,7 +77,8 @@ namespace ShardingCore.Sharding
private DbContextOptions<T> CreateMonopolyDbContextOptions() private DbContextOptions<T> CreateMonopolyDbContextOptions()
{ {
var dbContextOptionBuilder = CreateDbContextOptionBuilder(); var dbContextOptionBuilder = CreateDbContextOptionBuilder();
_shardingDbContextOptionsBuilderConfig.UseDbContextOptionsBuilder(dbContextOptionBuilder); var connectionString = Database.GetDbConnection().ConnectionString;
_shardingDbContextOptionsBuilderConfig.UseDbContextOptionsBuilder(connectionString,dbContextOptionBuilder);
return dbContextOptionBuilder.Options; return dbContextOptionBuilder.Options;
} }

View File

@ -16,13 +16,13 @@ namespace ShardingCore.Sharding
*/ */
public class ShardingDbContextOptionsBuilderConfig<TShardingDbContext> : IShardingDbContextOptionsBuilderConfig where TShardingDbContext : DbContext, IShardingDbContext public class ShardingDbContextOptionsBuilderConfig<TShardingDbContext> : IShardingDbContextOptionsBuilderConfig where TShardingDbContext : DbContext, IShardingDbContext
{ {
public ShardingDbContextOptionsBuilderConfig(Action<DbConnection, DbContextOptionsBuilder> sameConnectionDbContextOptionsCreator, Action<DbContextOptionsBuilder> defaultQueryDbContextOptionsCreator) public ShardingDbContextOptionsBuilderConfig(Action<DbConnection, DbContextOptionsBuilder> sameConnectionDbContextOptionsCreator, Action<string,DbContextOptionsBuilder> defaultQueryDbContextOptionsCreator)
{ {
SameConnectionDbContextOptionsCreator = sameConnectionDbContextOptionsCreator; SameConnectionDbContextOptionsCreator = sameConnectionDbContextOptionsCreator;
DefaultQueryDbContextOptionsCreator = defaultQueryDbContextOptionsCreator; DefaultQueryDbContextOptionsCreator = defaultQueryDbContextOptionsCreator;
} }
public Action<DbConnection, DbContextOptionsBuilder> SameConnectionDbContextOptionsCreator { get; } public Action<DbConnection, DbContextOptionsBuilder> SameConnectionDbContextOptionsCreator { get; }
public Action<DbContextOptionsBuilder> DefaultQueryDbContextOptionsCreator { get; } public Action<string,DbContextOptionsBuilder> DefaultQueryDbContextOptionsCreator { get; }
public Type ShardingDbContextType => typeof(TShardingDbContext); public Type ShardingDbContextType => typeof(TShardingDbContext);
public DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder) public DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder)
@ -32,9 +32,9 @@ namespace ShardingCore.Sharding
return dbContextOptionsBuilder; return dbContextOptionsBuilder;
} }
public DbContextOptionsBuilder UseDbContextOptionsBuilder(DbContextOptionsBuilder dbContextOptionsBuilder) public DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString,DbContextOptionsBuilder dbContextOptionsBuilder)
{ {
DefaultQueryDbContextOptionsCreator(dbContextOptionsBuilder); DefaultQueryDbContextOptionsCreator(connectionString,dbContextOptionsBuilder);
dbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>(); dbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>();
return dbContextOptionsBuilder; return dbContextOptionsBuilder;
} }

View File

@ -26,14 +26,14 @@ namespace ShardingCore
private readonly Dictionary<Type, Type> _virtualRoutes = new Dictionary<Type, Type>(); private readonly Dictionary<Type, Type> _virtualRoutes = new Dictionary<Type, Type>();
public Action<DbConnection, DbContextOptionsBuilder> SameConnectionConfigure { get; set; } public Action<DbConnection, DbContextOptionsBuilder> SameConnectionConfigure { get; set; }
public Action<DbContextOptionsBuilder> DefaultQueryConfigure { get; set; } public Action<string,DbContextOptionsBuilder> DefaultQueryConfigure { get; set; }
/// <summary> /// <summary>
/// 配置数据库分表查询和保存时的DbContext创建方式 /// 配置数据库分表查询和保存时的DbContext创建方式
/// </summary> /// </summary>
/// <param name="sameConnectionConfigure">DbConnection下如何配置因为不同的DbContext支持事务需要使用同一个DbConnection</param> /// <param name="sameConnectionConfigure">DbConnection下如何配置因为不同的DbContext支持事务需要使用同一个DbConnection</param>
/// <param name="defaultBuilderConfigure">默认查询DbContext创建的配置</param> /// <param name="defaultBuilderConfigure">默认查询DbContext创建的配置</param>
public void UseShardingOptionsBuilder(Action<DbConnection, DbContextOptionsBuilder> sameConnectionConfigure, Action<DbContextOptionsBuilder> defaultQueryConfigure = null) public void UseShardingOptionsBuilder(Action<DbConnection, DbContextOptionsBuilder> sameConnectionConfigure, Action<string,DbContextOptionsBuilder> defaultQueryConfigure = null)
{ {
SameConnectionConfigure = sameConnectionConfigure ?? throw new ArgumentNullException(nameof(sameConnectionConfigure)); SameConnectionConfigure = sameConnectionConfigure ?? throw new ArgumentNullException(nameof(sameConnectionConfigure));
DefaultQueryConfigure = defaultQueryConfigure ?? throw new ArgumentNullException(nameof(defaultQueryConfigure)); DefaultQueryConfigure = defaultQueryConfigure ?? throw new ArgumentNullException(nameof(defaultQueryConfigure));

View File

@ -1,5 +1,9 @@
{ {
"SqlServer": { "SqlServer": {
"ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;" "ConnectionString": "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;"
},
"MySql": {
"ConnectionString": "server=127.0.0.1;port=3306;user=root;password=root;database=ShardingCoreDB;sslMode=None;"
} }
} }

View File

@ -9,6 +9,7 @@
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.2" /> <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.1" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Xunit.DependencyInjection" Version="7.1.0" /> <PackageReference Include="Xunit.DependencyInjection" Version="7.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

View File

@ -51,13 +51,23 @@ namespace ShardingCore.Test50
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]) services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
,op => ,op =>
{ {
op.EnsureCreatedWithOutShardingTable = false; op.EnsureCreatedWithOutShardingTable = true;
op.CreateShardingTableOnStart = false; op.CreateShardingTableOnStart = true;
op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger), op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),
builder=> builder.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]).UseLoggerFactory(efLogger)); (conStr,builder)=> builder.UseSqlServer(conStr).UseLoggerFactory(efLogger));
op.AddShardingTableRoute<SysUserModVirtualTableRoute>(); op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>(); op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
}); });
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// ,op =>
// {
// op.EnsureCreatedWithOutShardingTable = true;
// op.CreateShardingTableOnStart = true;
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// });
} }
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法

View File

@ -23,7 +23,6 @@
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content> </Content>
<None Remove="Configs\MacDbConfig.json" /> <None Remove="Configs\MacDbConfig.json" />
<Content Include="Configs\MacDbConfig.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -44,10 +44,10 @@ namespace ShardingCore.Test50_2x
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]) services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
,op => ,op =>
{ {
op.EnsureCreatedWithOutShardingTable = false; op.EnsureCreatedWithOutShardingTable = true;
op.CreateShardingTableOnStart = false; op.CreateShardingTableOnStart = true;
op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger), op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),
builder=> builder.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]).UseLoggerFactory(efLogger)); (conStr,builder)=> builder.UseSqlServer(conStr).UseLoggerFactory(efLogger));
op.AddShardingTableRoute<SysUserModVirtualTableRoute>(); op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>(); op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
}); });

View File

@ -22,7 +22,6 @@
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content> </Content>
<None Remove="Configs\MacDbConfig.json" /> <None Remove="Configs\MacDbConfig.json" />
<Content Include="Configs\MacDbConfig.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -44,10 +44,10 @@ namespace ShardingCore.Test50_3x
services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]) services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])
,op => ,op =>
{ {
op.EnsureCreatedWithOutShardingTable = false; op.EnsureCreatedWithOutShardingTable = true;
op.CreateShardingTableOnStart = false; op.CreateShardingTableOnStart = true;
op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger), op.UseShardingOptionsBuilder((connection, builder) => builder.UseSqlServer(connection).UseLoggerFactory(efLogger),
builder=> builder.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"]).UseLoggerFactory(efLogger)); (conStr,builder)=> builder.UseSqlServer(conStr).UseLoggerFactory(efLogger));
op.AddShardingTableRoute<SysUserModVirtualTableRoute>(); op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>(); op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
}); });