diff --git a/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs b/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs
index 6ad799be..b807b952 100644
--- a/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs
+++ b/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs
@@ -61,6 +61,7 @@ namespace Sample.AutoCreateIfPresent
public override string ShardingKeyToTail(object shardingKey)
{
+ InitOnce();
return $"{shardingKey}";
}
@@ -70,6 +71,12 @@ namespace Sample.AutoCreateIfPresent
///
///
public override List GetTails()
+ {
+ InitOnce();
+ return _tails.Keys.ToList();
+ }
+
+ private void InitOnce()
{
if (!_inited)
{
@@ -82,8 +89,7 @@ namespace Sample.AutoCreateIfPresent
}
}
}
-
- return _tails.Keys.ToList();
+
}
public override void Configure(EntityMetadataTableBuilder builder)
diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs
index e3a76af8..e91b00a0 100644
--- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs
+++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs
@@ -85,7 +85,9 @@ namespace Sample.MySql.Controllers
var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
//系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
- var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod));
+ var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod));
+ var tails = tableRoute.GetTails();
+ var virtualTableRoute = (ITailAppendable)tableRoute;
//一定要先在路由里面添加尾巴
virtualTableRoute.Append("20220921");
shardingTableCreator.CreateTable("ds0","20220921");
@@ -330,6 +332,13 @@ namespace Sample.MySql.Controllers
[HttpGet]
public async Task Get10()
{
+ var shardingDbContextExecutor = _defaultTableDbContext.GetShardingExecutor();
+ var dataSourceDbContexts = shardingDbContextExecutor.GetCurrentDbContexts();
+ var sourceDbContexts = dataSourceDbContexts.Values;
+ var dataSourceDbContext = sourceDbContexts.FirstOrDefault();
+ var dbConnection = dataSourceDbContext.GetCurrentContexts().FirstOrDefault().Value.Database.GetDbConnection();
+
+
var sysUserMod1 = await _defaultTableDbContext.Set().Where(o=>o.UserId=="11231").AllAsync(o=>o.Id=="1123"&&string.Compare(o.UserId,"123")>0);
var sysUserMod2 = await _defaultTableDbContext.Set().AllAsync(o=>o.Id=="1123");
var dateTime = new DateTime(2020,1,1);
@@ -354,5 +363,25 @@ namespace Sample.MySql.Controllers
return Ok();
}
+ [HttpGet]
+ public async Task Get12()
+ {
+ var sysTests = from ut in _defaultTableDbContext.Set()
+ where _defaultTableDbContext.Set().Any(x => x.Id == ut.Id)
+ select ut;
+ var tests = sysTests.ToList();
+ return Ok();
+ }
+ [HttpGet]
+ public async Task Get13()
+ {
+ var list =await _defaultTableDbContext.Set()
+ .GroupBy(o=>o.Time)
+ .Select(o=>new
+ {
+ o.Key
+ }).ToListAsync();
+ return Ok();
+ }
}
}
diff --git a/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs b/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs
index 19f6436b..c0ff8641 100644
--- a/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs
+++ b/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs
@@ -9,6 +9,6 @@ namespace Sample.MySql.Domain.Entities
public class SysUserLogByMonth
{
public string Id { get; set; }
- public DateTime Time { get; set; }
+ public DateTime? Time { get; set; }
}
}
diff --git a/samples/Sample.MySql/Domain/Maps/SysTestMap.cs b/samples/Sample.MySql/Domain/Maps/SysTestMap.cs
index 1e0a4f47..1e7f65e3 100644
--- a/samples/Sample.MySql/Domain/Maps/SysTestMap.cs
+++ b/samples/Sample.MySql/Domain/Maps/SysTestMap.cs
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Sample.MySql.Domain.Entities;
namespace Sample.MySql.Domain.Maps
@@ -12,7 +13,7 @@ namespace Sample.MySql.Domain.Maps
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
builder.Property(o => o.UserId).IsRequired().HasMaxLength(128);
builder.Property(o => o.UserId).IsConcurrencyToken();
- builder.ToTable(nameof(SysTest));
+ builder.ToTable(nameof(SysTest),"dbdbd0");
}
}
}
\ No newline at end of file
diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs
index 43cb06b3..f4c8d500 100644
--- a/samples/Sample.MySql/Startup.cs
+++ b/samples/Sample.MySql/Startup.cs
@@ -74,6 +74,8 @@ namespace Sample.MySql
services.AddShardingDbContext()
.UseRouteConfig((sp, o) =>
{
+
+ // AppDomain.CurrentDomain.GetAssemblies().
o.AddShardingTableRoute();
o.AddShardingTableRoute();
o.AddShardingTableRoute();
@@ -93,6 +95,7 @@ namespace Sample.MySql
// {
// b.UseMemoryCache(memoryCache);
// });
+ o.CheckShardingKeyValueGenerated = false;
o.IgnoreCreateTableError = false;
o.UseEntityFrameworkCoreProxies = true;
o.CacheModelLockConcurrencyLevel = 1024;
@@ -107,6 +110,8 @@ namespace Sample.MySql
o.UseShardingQuery((conStr, builder) =>
{
+ var logger = sp.ApplicationServiceProvider.GetService>();
+ logger.LogInformation(conStr);
builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
.UseLoggerFactory(loggerFactory1)
.EnableSensitiveDataLogging();
@@ -180,7 +185,7 @@ namespace Sample.MySql
{
app.UseDeveloperExceptionPage();
}
- // app.ApplicationServices.UseAutoTryCompensateTable();
+ app.ApplicationServices.UseAutoTryCompensateTable();
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService();
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
diff --git a/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs b/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs
index 60dc9b9c..74080ce9 100644
--- a/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs
+++ b/src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
@@ -20,6 +21,24 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
///
internal static class AggregateExtension
{
+
+ public static object ChangeType(this object value, Type targetType)
+ {
+ //targetType类型是否为泛型,因为nullable是泛型类,
+ if (targetType.IsGenericType &&
+ //判断targetType是否为nullable泛型类
+ targetType.IsNullableType())
+ {
+ if (value == null)
+ {
+ return null;
+ }
+
+ NullableConverter nullableConverter = new NullableConverter(targetType);
+ targetType = nullableConverter.UnderlyingType;
+ }
+ return Convert.ChangeType(value, targetType);
+ }
public static TSource CopyTSource(TSource source)
{
var anonType = source.GetType();
@@ -27,9 +46,20 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
var allPropertyTypes= allProperties.Select(o=>o.PropertyType).ToArray();
if (anonType.IsAnonymousType())
{
- var constantExpressions = allProperties.Select(o => Expression.Constant(o.GetValue(source))).ToArray();
+ var constantExpressions = allProperties.Select(o =>
+ {
+ var value = o.GetValue(source);
+ if (value != null &&o.PropertyType.IsNullableType())
+ {
+ // var changeTypeValue = ChangeType(value,o.PropertyType);
+ return Expression.Constant(value,o.PropertyType);
+ }
+
+ return Expression.Constant(value);
+ }).ToArray();
+ var constructorInfo = anonType.GetConstructor(allPropertyTypes);
var exp = Expression.New(
- anonType.GetConstructor(allPropertyTypes),
+ constructorInfo,
constantExpressions);
var lambda = LambdaExpression.Lambda(exp);
TSource myObj = (TSource)lambda.Compile().DynamicInvoke();