修复 efcore2.x 无法重新编译的bug

This commit is contained in:
xuejiaming 2021-03-22 14:43:36 +08:00
parent e1515b052d
commit 26d559bba7
12 changed files with 92 additions and 10 deletions

View File

@ -1,8 +1,8 @@
:start
::定义版本
set EFCORE2=2.1.0.5
set EFCORE3=3.1.0.5
set EFCORE5=5.1.0.5
set EFCORE2=2.1.0.6
set EFCORE3=3.1.0.6
set EFCORE5=5.1.0.6
::删除所有bin与obj下的文件
@echo off

View File

@ -28,6 +28,9 @@ namespace Sample.MySql.Controllers
{
var result = await _virtualDbContext.Set<SysTest>().AnyAsync();
var result1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").ToShardingListAsync();
var shardingCountAsync = await _virtualDbContext.Set<SysUserMod>().ShardingCountAsync();
var shardingCountAsyn2c = await _virtualDbContext.Set<SysUserLogByMonth>().ShardingCountAsync();
return Ok(result1);
}
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Builder;
@ -42,8 +43,18 @@ namespace Sample.MySql
Name = $"name_{id}",
});
}
var userModMonths = new List<SysUserLogByMonth>();
foreach (var id in ids)
{
userModMonths.Add(new SysUserLogByMonth()
{
Id = id.ToString(),
Time = DateTime.Now
});
}
virtualDbContext.InsertRange(userMods);
virtualDbContext.InsertRange(userModMonths);
virtualDbContext.SaveChanges();
}
}

View File

@ -11,11 +11,13 @@ namespace Sample.MySql.DbContexts
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new SysUserModMap());
modelBuilder.ApplyConfiguration(new SysTestMap());
modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap());
}
public string ModelChangeKey { get; set; }

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ShardingCore.Core;
namespace Sample.MySql.Domain.Entities
{
public class SysUserLogByMonth : IShardingTable
{
public string Id { get; set; }
[ShardingTableKey]
public DateTime Time { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sample.MySql.Domain.Entities;
namespace Sample.MySql.Domain.Maps
{
public class SysUserLogByMonthMap : IEntityTypeConfiguration<SysUserLogByMonth>
{
public void Configure(EntityTypeBuilder<SysUserLogByMonth> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
builder.ToTable(nameof(SysUserLogByMonth));
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sample.MySql.Domain.Entities;
using ShardingCore.VirtualRoutes.Months;
namespace Sample.MySql.Shardings
{
public class SysUserLogByMonthRoute:AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<SysUserLogByMonth>
{
public override DateTime GetBeginTime()
{
return new DateTime(2021, 3, 01);
}
}
}

View File

@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sample.MySql.DbContexts;
using Sample.MySql.Shardings;
using ShardingCore.MySql;
@ -32,13 +33,16 @@ namespace Sample.MySql
{
o.EnsureCreatedWithOutShardingTable = true;
o.CreateShardingTableOnStart = true;
o.AddShardingDbContextWithShardingTable<DefaultTableDbContext>("conn1", "server=xxxx;userid=xxxx;password=xxxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig =>
o.AddShardingDbContextWithShardingTable<DefaultTableDbContext>("conn1", "server=xxx;userid=xxx;password=xxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig =>
{
dbConfig.AddShardingTableRoute<SysUserModVirtualTableRoute>();
dbConfig.AddShardingTableRoute<SysUserLogByMonthRoute>();
});
//o.AddDataSourceVirtualRoute<>();
});
services.AddLogging(b => b.AddConsole());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -2,7 +2,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},

View File

@ -1,4 +1,5 @@
#if !EFCORE2
using System;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;
@ -89,7 +90,6 @@ namespace ShardingCore.MySql.EFCores
{
newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{tailPrefix}{tail}", tableExpression.Schema)}")) + ")";
}
var relationalCommandBuilder = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_relationalCommandBuilder") as IRelationalCommandBuilder;
relationalCommandBuilder.Append(newTableName).Append(this.AliasSeparator).Append(sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));
return tableExpression;
@ -105,6 +105,7 @@ namespace ShardingCore.MySql.EFCores
#endif
#if EFCORE2
using System;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore;
@ -204,7 +205,6 @@ namespace ShardingCore.MySql.EFCores
return tableExpression;
}
}
var result = base.VisitTable(tableExpression);
return result;
}

View File

@ -44,10 +44,12 @@ namespace ShardingCore.Core.Internal.StreamMerge.GenericMerges
ICollection<DbContext> parallelDbContexts = new LinkedList<DbContext>();
try
{
var intersectConfigs = _mergeContext.GetDataSourceRoutingResult().IntersectConfigs;
var enumeratorTasks = _mergeContext.GetDataSourceRoutingResult().IntersectConfigs.SelectMany(connectKey =>
var enumeratorTasks = intersectConfigs.SelectMany(connectKey =>
{
return _mergeContext.GetRouteResults(connectKey).Select(routeResult =>
var routeResults = _mergeContext.GetRouteResults(connectKey);
return routeResults.Select(routeResult =>
{
return Task.Run(async () =>
{
@ -58,7 +60,7 @@ namespace ShardingCore.Core.Internal.StreamMerge.GenericMerges
return await EFCoreExecute(connectKey,newQueryable, routeResult, efQuery);
});
});
}).ToList();
}).ToArray();
return (await Task.WhenAll(enumeratorTasks)).ToList();

View File

@ -165,6 +165,7 @@ namespace ShardingCore.EFCores
var compiledQuery
= CompileQueryCore<TResult>(query, _queryModelGenerator, _database, _logger, _contextType);
return compiledQuery(queryContext);
}
@ -190,6 +191,13 @@ namespace ShardingCore.EFCores
public override Task<TResult> ExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken)
{
var shardingAccessor = ShardingContainer.Services.GetService<IShardingAccessor>();
if (shardingAccessor?.ShardingContext != null)
{
return ShardingExecuteAsync<TResult>(query, cancellationToken);
}
return base.ExecuteAsync<TResult>(query, cancellationToken);
}
private Task<TResult> ShardingExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken)