7.x.1.10 修复匿名对象返回nullable的value的bug

This commit is contained in:
xuejiaming 2023-06-06 09:14:50 +08:00
parent b058b66a3a
commit 6d2253680b
6 changed files with 79 additions and 8 deletions

View File

@ -61,6 +61,7 @@ namespace Sample.AutoCreateIfPresent
public override string ShardingKeyToTail(object shardingKey)
{
InitOnce();
return $"{shardingKey}";
}
@ -70,6 +71,12 @@ namespace Sample.AutoCreateIfPresent
/// </summary>
/// <returns></returns>
public override List<string> GetTails()
{
InitOnce();
return _tails.Keys.ToList();
}
private void InitOnce()
{
if (!_inited)
{
@ -83,7 +90,6 @@ namespace Sample.AutoCreateIfPresent
}
}
return _tails.Keys.ToList();
}
public override void Configure(EntityMetadataTableBuilder<AreaDevice> builder)

View File

@ -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<SysUserMod>("ds0","20220921");
@ -330,6 +332,13 @@ namespace Sample.MySql.Controllers
[HttpGet]
public async Task<IActionResult> 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<SysTest>().Where(o=>o.UserId=="11231").AllAsync(o=>o.Id=="1123"&&string.Compare(o.UserId,"123")>0);
var sysUserMod2 = await _defaultTableDbContext.Set<SysTest>().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<IActionResult> Get12()
{
var sysTests = from ut in _defaultTableDbContext.Set<SysTest>()
where _defaultTableDbContext.Set<SysUserLogByMonth>().Any(x => x.Id == ut.Id)
select ut;
var tests = sysTests.ToList();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get13()
{
var list =await _defaultTableDbContext.Set<SysUserLogByMonth>()
.GroupBy(o=>o.Time)
.Select(o=>new
{
o.Key
}).ToListAsync();
return Ok();
}
}
}

View File

@ -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; }
}
}

View File

@ -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");
}
}
}

View File

@ -74,6 +74,8 @@ namespace Sample.MySql
services.AddShardingDbContext<DefaultShardingDbContext>()
.UseRouteConfig((sp, o) =>
{
// AppDomain.CurrentDomain.GetAssemblies().
o.AddShardingTableRoute<DynamicTableRoute>();
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
@ -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<ILogger<Startup>>();
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<IShardingRuntimeContext>();
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();

View File

@ -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
/// </summary>
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>(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();