Skip to content

Commit

Permalink
发布x.6.0.3优化迁移的使用方式
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed Jul 6, 2022
1 parent 128c096 commit b5aa66f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
8 changes: 4 additions & 4 deletions nuget-publish.bat
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 31 additions & 10 deletions samples/Sample.MySql/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,16 +28,35 @@ public WeatherForecastController(DefaultShardingDbContext defaultTableDbContext)
[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()
Expand Down
13 changes: 4 additions & 9 deletions samples/Sample.MySql/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ public void ConfigureServices(IServiceCollection services)
{"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>();
Expand Down Expand Up @@ -164,10 +159,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}
}

// 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();
Expand Down
2 changes: 1 addition & 1 deletion src/ShardingCore/Extensions/ShardingRuntimeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void UseAutoTryCompensateTable(this IShardingRuntimeContext shardi
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);
Expand Down
10 changes: 10 additions & 0 deletions src/ShardingCore/ShardingCoreExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -102,10 +103,13 @@ public static void UseDefaultSharding<TShardingDbContext>(IServiceProvider servi
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);
Expand Down Expand Up @@ -198,6 +202,12 @@ public static DbContextOptionsBuilder UseSharding<TShardingDbContext>(
.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)
Expand Down

0 comments on commit b5aa66f

Please sign in to comment.