发布x.6.0.3优化迁移的使用方式

This commit is contained in:
xuejiaming 2022-07-06 22:50:16 +08:00
parent 128c096d90
commit b5aa66f37a
5 changed files with 50 additions and 24 deletions

View File

@ -1,9 +1,9 @@
:start :start
::定义版本 ::定义版本
set EFCORE2=2.6.0.2 set EFCORE2=2.6.0.3
set EFCORE3=3.6.0.2 set EFCORE3=3.6.0.3
set EFCORE5=5.6.0.2 set EFCORE5=5.6.0.3
set EFCORE6=6.6.0.2 set EFCORE6=6.6.0.3
::删除所有bin与obj下的文件 ::删除所有bin与obj下的文件
@echo off @echo off

View File

@ -2,9 +2,11 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Sample.MySql.DbContexts; using Sample.MySql.DbContexts;
using Sample.MySql.Domain.Entities; using Sample.MySql.Domain.Entities;
using ShardingCore.TableCreator; using ShardingCore.TableCreator;
@ -26,16 +28,35 @@ namespace Sample.MySql.Controllers
[HttpGet] [HttpGet]
public async Task<IActionResult> Get() public async Task<IActionResult> Get()
{ {
var resultX = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync(); // using (var tran = _defaultTableDbContext.Database.BeginTransaction())
var resultY = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3"); // {
var result = await _defaultTableDbContext.Set<SysTest>().AnyAsync();
var result1 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").ToListAsync(); var resultX = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync();
var result2 = await _defaultTableDbContext.Set<SysUserLogByMonth>().Skip(1).Take(10).ToListAsync(); var resultY = await _defaultTableDbContext.Set<SysUserMod>().FirstOrDefaultAsync(o => o.Id == "2" || o.Id == "3");
var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set<SysUserLogByMonth>().ToListAsync(); var result = await _defaultTableDbContext.Set<SysTest>().AnyAsync();
var shardingCountAsync = await _defaultTableDbContext.Set<SysUserMod>().CountAsync(); var result1 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").ToListAsync();
var shardingCountAsyn2c = _defaultTableDbContext.Set<SysUserLogByMonth>().Count(); var result2 = await _defaultTableDbContext.Set<SysUserLogByMonth>().Skip(1).Take(10).ToListAsync();
var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set<SysUserLogByMonth>().ToListAsync();
return Ok(result1); var shardingCountAsync = await _defaultTableDbContext.Set<SysUserMod>().CountAsync();
var shardingCountAsyn2c = _defaultTableDbContext.Set<SysUserLogByMonth>().Count();
// var dbConnection = _defaultTableDbContext.Database.GetDbConnection();
// if (dbConnection.State != ConnectionState.Open)
// {
// dbConnection.Open();
// }
// using (var dbCommand = dbConnection.CreateCommand())
// {
// dbCommand.CommandText = "select * from systest";
// dbCommand.Transaction = _defaultTableDbContext.Database.CurrentTransaction?.GetDbTransaction();
// var dbDataReader = dbCommand.ExecuteReader();
// while (dbDataReader.Read())
// {
// Console.WriteLine(dbDataReader[0]);
// }
// }
// }
return Ok(1);
} }
// [HttpGet] // [HttpGet]
// public async Task<IActionResult> Get1() // public async Task<IActionResult> Get1()

View File

@ -93,11 +93,6 @@ namespace Sample.MySql
{"ds1", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;"}, {"ds1", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;"},
{"ds2", "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;"} {"ds2", "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;"}
}); });
o.UseShellDbContextConfigure(b =>
{
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
b.ReplaceService<IMigrator, ShardingMigrator>();
});
o.UseShardingMigrationConfigure(b => o.UseShardingMigrationConfigure(b =>
{ {
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>(); b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
@ -164,10 +159,10 @@ namespace Sample.MySql
} }
} }
// Stopwatch sp = Stopwatch.StartNew(); Stopwatch sp = Stopwatch.StartNew();
// app.ApplicationServices.UseAutoTryCompensateTable(); app.ApplicationServices.UseAutoTryCompensateTable();
// sp.Stop(); sp.Stop();
// Console.WriteLine("UseAutoTryCompensateTable:"+sp.ElapsedMilliseconds); Console.WriteLine("UseAutoTryCompensateTable:"+sp.ElapsedMilliseconds);
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();

View File

@ -25,7 +25,7 @@ namespace ShardingCore.Extensions
var compensateTableParallelCount = parallelCount ?? shardingConfigOptions.CompensateTableParallelCount; var compensateTableParallelCount = parallelCount ?? shardingConfigOptions.CompensateTableParallelCount;
if (compensateTableParallelCount <= 0) if (compensateTableParallelCount <= 0)
{ {
throw new ShardingCoreInvalidOperationException($"compensate table parallel count must >0"); throw new ShardingCoreInvalidOperationException($"compensate table parallel count must > 0");
} }
var allDataSourceNames = virtualDataSource.GetAllDataSourceNames(); var allDataSourceNames = virtualDataSource.GetAllDataSourceNames();
var partitionMigrationUnits = allDataSourceNames.Partition(compensateTableParallelCount); var partitionMigrationUnits = allDataSourceNames.Partition(compensateTableParallelCount);

View File

@ -27,6 +27,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Migrations;
using ShardingCore.Bootstrappers; using ShardingCore.Bootstrappers;
using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.QueryTrackers; using ShardingCore.Core.QueryTrackers;
@ -102,10 +103,13 @@ namespace ShardingCore
DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
{ {
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>(); var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions();
shardingConfigOptions.ShardingMigrationConfigure?.Invoke(dbContextOptionsBuilder);
var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName); var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName);
var contextOptionsBuilder = virtualDataSource.ConfigurationParams var contextOptionsBuilder = virtualDataSource.ConfigurationParams
.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder) .UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder)
.UseShardingMigrator()
.UseSharding<TShardingDbContext>(shardingRuntimeContext); .UseSharding<TShardingDbContext>(shardingRuntimeContext);
virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder); virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder);
@ -198,6 +202,12 @@ namespace ShardingCore
.ReplaceService<IRelationalTransactionFactory, .ReplaceService<IRelationalTransactionFactory,
ShardingRelationalTransactionFactory<TShardingDbContext>>(); ShardingRelationalTransactionFactory<TShardingDbContext>>();
} }
public static DbContextOptionsBuilder UseShardingMigrator(
this DbContextOptionsBuilder optionsBuilder)
{
return optionsBuilder
.ReplaceService<IMigrator, ShardingMigrator>();
}
public static DbContextOptionsBuilder UseShardingOptions(this DbContextOptionsBuilder optionsBuilder, public static DbContextOptionsBuilder UseShardingOptions(this DbContextOptionsBuilder optionsBuilder,
IShardingRuntimeContext shardingRuntimeContext) IShardingRuntimeContext shardingRuntimeContext)