发布x.6.0.3优化迁移的使用方式
This commit is contained in:
parent
128c096d90
commit
b5aa66f37a
|
@ -1,9 +1,9 @@
|
|||
:start
|
||||
::定义版本
|
||||
set EFCORE2=2.6.0.2
|
||||
set EFCORE3=3.6.0.2
|
||||
set EFCORE5=5.6.0.2
|
||||
set EFCORE6=6.6.0.2
|
||||
set EFCORE2=2.6.0.3
|
||||
set EFCORE3=3.6.0.3
|
||||
set EFCORE5=5.6.0.3
|
||||
set EFCORE6=6.6.0.3
|
||||
|
||||
::删除所有bin与obj下的文件
|
||||
@echo off
|
||||
|
|
|
@ -2,9 +2,11 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Sample.MySql.DbContexts;
|
||||
using Sample.MySql.Domain.Entities;
|
||||
using ShardingCore.TableCreator;
|
||||
|
@ -26,16 +28,35 @@ namespace Sample.MySql.Controllers
|
|||
[HttpGet]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
var resultX = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync();
|
||||
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 result2 = await _defaultTableDbContext.Set<SysUserLogByMonth>().Skip(1).Take(10).ToListAsync();
|
||||
var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set<SysUserLogByMonth>().ToListAsync();
|
||||
var shardingCountAsync = await _defaultTableDbContext.Set<SysUserMod>().CountAsync();
|
||||
var shardingCountAsyn2c = _defaultTableDbContext.Set<SysUserLogByMonth>().Count();
|
||||
|
||||
return Ok(result1);
|
||||
// using (var tran = _defaultTableDbContext.Database.BeginTransaction())
|
||||
// {
|
||||
|
||||
var resultX = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").FirstOrDefaultAsync();
|
||||
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 result2 = await _defaultTableDbContext.Set<SysUserLogByMonth>().Skip(1).Take(10).ToListAsync();
|
||||
var shardingFirstOrDefaultAsync = await _defaultTableDbContext.Set<SysUserLogByMonth>().ToListAsync();
|
||||
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]
|
||||
// public async Task<IActionResult> Get1()
|
||||
|
|
|
@ -93,11 +93,6 @@ namespace Sample.MySql
|
|||
{"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;"}
|
||||
});
|
||||
o.UseShellDbContextConfigure(b =>
|
||||
{
|
||||
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
|
||||
b.ReplaceService<IMigrator, ShardingMigrator>();
|
||||
});
|
||||
o.UseShardingMigrationConfigure(b =>
|
||||
{
|
||||
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
|
||||
|
@ -164,10 +159,10 @@ namespace Sample.MySql
|
|||
}
|
||||
}
|
||||
|
||||
// Stopwatch sp = Stopwatch.StartNew();
|
||||
// app.ApplicationServices.UseAutoTryCompensateTable();
|
||||
// sp.Stop();
|
||||
// Console.WriteLine("UseAutoTryCompensateTable:"+sp.ElapsedMilliseconds);
|
||||
Stopwatch sp = Stopwatch.StartNew();
|
||||
app.ApplicationServices.UseAutoTryCompensateTable();
|
||||
sp.Stop();
|
||||
Console.WriteLine("UseAutoTryCompensateTable:"+sp.ElapsedMilliseconds);
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace ShardingCore.Extensions
|
|||
var compensateTableParallelCount = parallelCount ?? shardingConfigOptions.CompensateTableParallelCount;
|
||||
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 partitionMigrationUnits = allDataSourceNames.Partition(compensateTableParallelCount);
|
||||
|
|
|
@ -27,6 +27,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using ShardingCore.Bootstrappers;
|
||||
using ShardingCore.Core.DbContextCreator;
|
||||
using ShardingCore.Core.QueryTrackers;
|
||||
|
@ -102,10 +103,13 @@ namespace ShardingCore
|
|||
DbContextOptionsBuilder dbContextOptionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
|
||||
var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions();
|
||||
shardingConfigOptions.ShardingMigrationConfigure?.Invoke(dbContextOptionsBuilder);
|
||||
var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
|
||||
var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName);
|
||||
var contextOptionsBuilder = virtualDataSource.ConfigurationParams
|
||||
.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder)
|
||||
.UseShardingMigrator()
|
||||
.UseSharding<TShardingDbContext>(shardingRuntimeContext);
|
||||
|
||||
virtualDataSource.ConfigurationParams.UseShellDbContextOptionBuilder(contextOptionsBuilder);
|
||||
|
@ -198,6 +202,12 @@ namespace ShardingCore
|
|||
.ReplaceService<IRelationalTransactionFactory,
|
||||
ShardingRelationalTransactionFactory<TShardingDbContext>>();
|
||||
}
|
||||
public static DbContextOptionsBuilder UseShardingMigrator(
|
||||
this DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
return optionsBuilder
|
||||
.ReplaceService<IMigrator, ShardingMigrator>();
|
||||
}
|
||||
|
||||
public static DbContextOptionsBuilder UseShardingOptions(this DbContextOptionsBuilder optionsBuilder,
|
||||
IShardingRuntimeContext shardingRuntimeContext)
|
||||
|
|
Loading…
Reference in New Issue