修复ExecuteUpdate Combine bug

This commit is contained in:
xuejiaming 2022-11-09 15:32:48 +08:00
parent ee1721da21
commit 354f623944
9 changed files with 80 additions and 22 deletions

View File

@ -390,6 +390,23 @@ namespace Sample.SqlServer.Controllers
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
return Ok(); return Ok();
} }
[HttpGet]
public async Task<IActionResult> Get8()
{
var resultx112331tt2 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "3").FirstOrDefaultAsync();
var xx=await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Id == "3")
.ExecuteUpdateAsync(
s => s.SetProperty(b => b.Name, b => b.Name + "1"));
var xx1 = await _defaultTableDbContext.Set<SysUserMod>().Where(o => o.Name == "name_3")
.ExecuteUpdateAsync(
s => s.SetProperty(b => b.Age, b => b.Age + 1));
return Ok();
}
} }
} }

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@ -7,16 +7,15 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="EFCore.BulkExtensions" Version="6.5.6" /> <PackageReference Include="EFCore.BulkExtensions" Version="6.5.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.10"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="6.0.10" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src2_6\ShardingCore.2_6\ShardingCore.2_6.csproj" /> <ProjectReference Include="..\..\src\ShardingCore\ShardingCore.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,32 @@
using System.Linq;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Update;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Helpers;
namespace Sample.SqlServer.Shardings
{
public class ShardingSqlServerMigrationsSqlGenerator:SqlServerMigrationsSqlGenerator
{
private readonly IShardingRuntimeContext _shardingRuntimeContext;
public ShardingSqlServerMigrationsSqlGenerator(IShardingRuntimeContext shardingRuntimeContext,MigrationsSqlGeneratorDependencies dependencies, ICommandBatchPreparer commandBatchPreparer) : base(dependencies, commandBatchPreparer)
{
_shardingRuntimeContext = shardingRuntimeContext;
}
protected override void Generate(
MigrationOperation operation,
IModel model,
MigrationCommandListBuilder builder)
{
var oldCmds = builder.GetCommandList().ToList();
base.Generate(operation, model, builder);
var newCmds = builder.GetCommandList().ToList();
var addCmds = newCmds.Where(x => !oldCmds.Contains(x)).ToList();
MigrationHelper.Generate(_shardingRuntimeContext, operation, builder, Dependencies.SqlGenerationHelper, addCmds);
}
}
}

View File

@ -56,8 +56,14 @@ namespace Sample.SqlServer
builder.UseLoggerFactory(loggerFactory).UseUnionAllMerge<DefaultShardingDbContext>(); builder.UseLoggerFactory(loggerFactory).UseUnionAllMerge<DefaultShardingDbContext>();
}); });
op.AddDefaultDataSource("A", op.AddDefaultDataSource("A",
"Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;" "Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;TrustServerCertificate=True;"
); );
op.UseShardingMigrationConfigure(o =>
{
o.ReplaceService<IMigrationsSqlGenerator, ShardingSqlServerMigrationsSqlGenerator>();
});
}).AddServiceConfigure(s => }).AddServiceConfigure(s =>
{ {
s.AddSingleton<ILoggerFactory>(sp => LoggerFactory.Create(builder => s.AddSingleton<ILoggerFactory>(sp => LoggerFactory.Create(builder =>
@ -121,9 +127,7 @@ namespace Sample.SqlServer
using (var serviceScope = app.ApplicationServices.CreateScope()) using (var serviceScope = app.ApplicationServices.CreateScope())
{ {
var defaultShardingDbContext = serviceScope.ServiceProvider.GetService<DefaultShardingDbContext>(); var defaultShardingDbContext = serviceScope.ServiceProvider.GetService<DefaultShardingDbContext>();
defaultShardingDbContext.Database.Migrate();
var migrator = defaultShardingDbContext.GetService<IMigrator>();
migrator.Migrate("InitialCreate");
} }
app.ApplicationServices.UseAutoTryCompensateTable(); app.ApplicationServices.UseAutoTryCompensateTable();

View File

@ -16,16 +16,18 @@ namespace Sample.SqlServer.UnionAllMerge
public class UnionAllMergeSqlServerQuerySqlGeneratorFactory<TShardingDbContext> : IQuerySqlGeneratorFactory, IUnionAllMergeQuerySqlGeneratorFactory public class UnionAllMergeSqlServerQuerySqlGeneratorFactory<TShardingDbContext> : IQuerySqlGeneratorFactory, IUnionAllMergeQuerySqlGeneratorFactory
where TShardingDbContext : DbContext, IShardingDbContext where TShardingDbContext : DbContext, IShardingDbContext
{ {
private readonly IRelationalTypeMappingSource _typeMappingSource;
private readonly IShardingRuntimeContext _shardingRuntimeContext; private readonly IShardingRuntimeContext _shardingRuntimeContext;
public UnionAllMergeSqlServerQuerySqlGeneratorFactory(QuerySqlGeneratorDependencies dependencies,IShardingRuntimeContext shardingRuntimeContext) public UnionAllMergeSqlServerQuerySqlGeneratorFactory(QuerySqlGeneratorDependencies dependencies, IRelationalTypeMappingSource typeMappingSource, IShardingRuntimeContext shardingRuntimeContext)
{ {
_typeMappingSource = typeMappingSource;
_shardingRuntimeContext = shardingRuntimeContext; _shardingRuntimeContext = shardingRuntimeContext;
Dependencies = dependencies; Dependencies = dependencies;
} }
public QuerySqlGeneratorDependencies Dependencies { get; } public QuerySqlGeneratorDependencies Dependencies { get; }
public QuerySqlGenerator Create() => new UnionAllMergeSqlServerQuerySqlGenerator<TShardingDbContext>(Dependencies,_shardingRuntimeContext); public QuerySqlGenerator Create() => new UnionAllMergeSqlServerQuerySqlGenerator<TShardingDbContext>(Dependencies, _typeMappingSource, _shardingRuntimeContext);
} }
public class UnionAllMergeSqlServerQuerySqlGenerator<TShardingDbContext> : SqlServerQuerySqlGenerator public class UnionAllMergeSqlServerQuerySqlGenerator<TShardingDbContext> : SqlServerQuerySqlGenerator
@ -35,13 +37,19 @@ namespace Sample.SqlServer.UnionAllMerge
private readonly IEntityMetadataManager _entityMetadataManager; private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IUnionAllMergeManager _unionAllMergeManager; private readonly IUnionAllMergeManager _unionAllMergeManager;
public UnionAllMergeSqlServerQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies,IShardingRuntimeContext shardingRuntimeContext) public UnionAllMergeSqlServerQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies, IRelationalTypeMappingSource typeMappingSource, IShardingRuntimeContext shardingRuntimeContext) : base(dependencies, typeMappingSource)
: base(dependencies)
{ {
_shardingRuntimeContext = shardingRuntimeContext; _shardingRuntimeContext = shardingRuntimeContext;
_entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
_unionAllMergeManager = shardingRuntimeContext.GetRequiredService<IUnionAllMergeManager>(); _unionAllMergeManager = shardingRuntimeContext.GetRequiredService<IUnionAllMergeManager>();
} }
//public UnionAllMergeSqlServerQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies,IShardingRuntimeContext shardingRuntimeContext)
// : base(dependencies)
//{
// _shardingRuntimeContext = shardingRuntimeContext;
// _entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
// _unionAllMergeManager = shardingRuntimeContext.GetRequiredService<IUnionAllMergeManager>();
//}
protected override Expression VisitTable(TableExpression tableExpression) protected override Expression VisitTable(TableExpression tableExpression)
{ {
@ -89,5 +97,6 @@ namespace Sample.SqlServer.UnionAllMerge
var result = base.VisitTable(tableExpression); var result = base.VisitTable(tableExpression);
return result; return result;
} }
} }
} }

View File

@ -12,7 +12,7 @@ namespace ShardingCore.Sharding.ShardingExecutors.QueryableCombines
{ {
public override IQueryable DoCombineQueryable(IQueryable queryable, Expression secondExpression, IQueryCompilerContext queryCompilerContext) public override IQueryable DoCombineQueryable(IQueryable queryable, Expression secondExpression, IQueryCompilerContext queryCompilerContext)
{ {
if (!(secondExpression is ConstantExpression)) if (!(secondExpression is UnaryExpression where && where.Operand is LambdaExpression lambdaExpression))
{ {
throw new ShardingCoreInvalidOperationException(queryCompilerContext.GetQueryExpression().ShardingPrint()); throw new ShardingCoreInvalidOperationException(queryCompilerContext.GetQueryExpression().ShardingPrint());
} }

View File

@ -95,7 +95,4 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.*" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.*" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.*" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Sharding\Visitors\GroupBys" />
</ItemGroup>
</Project> </Project>

View File

@ -282,7 +282,7 @@ namespace ShardingCore.Test
{ {
_virtualDbContext.ReadWriteSeparationReadOnly(); _virtualDbContext.ReadWriteSeparationReadOnly();
var connectionString1 = _connectionStringManager.GetConnectionString(_virtualDataSource.DefaultDataSourceName, true); var connectionString1 = _connectionStringManager.GetConnectionString(_virtualDataSource.DefaultDataSourceName, true);
Assert.Equal(connectionString1, "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;"); Assert.Equal(connectionString1, "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;TrustServerCertificate=True;");
_virtualDbContext.ReadWriteSeparationWriteOnly(); _virtualDbContext.ReadWriteSeparationWriteOnly();
} }
//[Fact] //[Fact]

View File

@ -81,14 +81,14 @@ namespace ShardingCore.Test
}); });
//添加默认数据源 //添加默认数据源
op.AddDefaultDataSource("A", op.AddDefaultDataSource("A",
"Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;"); "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;TrustServerCertificate=True;");
//添加额外数据源 //添加额外数据源
op.AddExtraDataSource(sp => op.AddExtraDataSource(sp =>
{ {
return new Dictionary<string, string>() return new Dictionary<string, string>()
{ {
{ "B", "Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;" }, { "B", "Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;TrustServerCertificate=True;" },
{ "C", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;" }, { "C", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;TrustServerCertificate=True;" },
}; };
}); });
//添加读写分离 //添加读写分离
@ -99,7 +99,7 @@ namespace ShardingCore.Test
{ {
"A", new HashSet<string>() "A", new HashSet<string>()
{ {
"Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;" "Data Source=localhost;Initial Catalog=ShardingCoreDBB;Integrated Security=True;TrustServerCertificate=True;"
} }
} }
}; };