还原shardingcore的创建dbcontext还是使用同链接

This commit is contained in:
xuejiaming 2022-06-23 07:44:26 +08:00
parent cbd55c9280
commit aaed91b0b8
4 changed files with 57 additions and 5 deletions

View File

@ -0,0 +1,39 @@
using Sample.MySql.Domain.Entities;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions;
namespace Sample.MySql.Shardings;
public class SysUserModVirtualDataSourceRoute:AbstractShardingOperatorVirtualDataSourceRoute<SysUserMod,string>
{
public override string ShardingKeyToDataSourceName(object shardingKey)
{
return $"{shardingKey}";
}
public override List<string> GetAllDataSourceNames()
{
return Enumerable.Range(0, 500).Select(o => $"ds{o}").ToList();
}
public override bool AddDataSourceName(string dataSourceName)
{
throw new NotImplementedException();
}
public override void Configure(EntityMetadataDataSourceBuilder<SysUserMod> builder)
{
builder.ShardingProperty(o => o.Name);
}
public override Func<string, bool> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator)
{
var shardingKeyToDataSourceName = ShardingKeyToDataSourceName(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.Equal: return t => t.Equals(shardingKeyToDataSourceName);
default: return t => true;
}
}
}

View File

@ -5,9 +5,11 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MySqlConnector;
using Sample.MySql.DbContexts;
using Sample.MySql.Shardings;
using ShardingCore;
using ShardingCore.Helpers;
using ShardingCore.TableExists;
namespace Sample.MySql
@ -49,6 +51,7 @@ namespace Sample.MySql
o.IgnoreCreateTableError = true;
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
o.UseShardingQuery((conStr, builder) =>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version())
@ -67,9 +70,9 @@ namespace Sample.MySql
})
.AddConfig(op =>
{
op.ConfigId = "c1";
op.ConfigId = "c0";
op.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3307;database=dbxxxx;userid=root;password=root;");
"server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;");
//op.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=db2;userid=root;password=L6yBtV6qNENrwBy7;")
op.ReplaceTableEnsureManager(sp=>new MySqlTableEnsureManager<DefaultShardingDbContext>());
@ -94,6 +97,16 @@ namespace Sample.MySql
{
endpoints.MapControllers();
});
// for (int i = 1; i < 500; i++)
// {
// using (var conn = new MySqlConnection(
// $"server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;"))
// {
// conn.Open();
// }
// // DynamicShardingHelper.DynamicAppendDataSource<DefaultShardingDbContext>($"c0",$"ds{i}",$"server=127.0.0.1;port=3306;database=dbdbd{i};userid=root;password=root;");
//
// }
app.DbSeed();
}
}

View File

@ -167,7 +167,7 @@ namespace ShardingCore.DynamicDataSources
if (context is IShardingDbContext shardingDbContext)
{
using (var dbContext =
shardingDbContext.GetDbContext(dataSourceName, true,
shardingDbContext.GetDbContext(dataSourceName, false,
_routeTailFactory.Create(string.Empty, false)))
{
var isDefault = virtualDataSource.IsDefault(dataSourceName);
@ -190,7 +190,7 @@ namespace ShardingCore.DynamicDataSources
{
if (context is IShardingDbContext shardingDbContext)
{
using (var dbContext = shardingDbContext.GetDbContext(dataSourceName, true,
using (var dbContext = shardingDbContext.GetDbContext(dataSourceName, false,
_routeTailFactory.Create(string.Empty, false)))
{
dbContext.RemoveDbContextAllRelationModel();

View File

@ -54,7 +54,7 @@ namespace ShardingCore.TableCreator
{
var shardingDbContext = (IShardingDbContext)dbContext;
using (var context = shardingDbContext.GetDbContext(dataSourceName, true,
using (var context = shardingDbContext.GetDbContext(dataSourceName, false,
_routeTailFactory.Create(tail, false)))
{
context.RemoveDbContextRelationModelSaveOnlyThatIsNamedType(shardingEntityType);