Skip to content

Commit 6d22536

Browse files
committed
7.x.1.10 修复匿名对象返回nullable的value的bug
1 parent b058b66 commit 6d22536

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private void InitTails()
6161

6262
public override string ShardingKeyToTail(object shardingKey)
6363
{
64+
InitOnce();
6465
return $"{shardingKey}";
6566
}
6667

@@ -70,6 +71,12 @@ public override string ShardingKeyToTail(object shardingKey)
7071
/// </summary>
7172
/// <returns></returns>
7273
public override List<string> GetTails()
74+
{
75+
InitOnce();
76+
return _tails.Keys.ToList();
77+
}
78+
79+
private void InitOnce()
7380
{
7481
if (!_inited)
7582
{
@@ -82,8 +89,7 @@ public override List<string> GetTails()
8289
}
8390
}
8491
}
85-
86-
return _tails.Keys.ToList();
92+
8793
}
8894

8995
public override void Configure(EntityMetadataTableBuilder<AreaDevice> builder)

samples/Sample.MySql/Controllers/WeatherForecastController.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public IQueryable<SysTest> GetAll()
8585
var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
8686
var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
8787
//系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
88-
var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod));
88+
var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod));
89+
var tails = tableRoute.GetTails();
90+
var virtualTableRoute = (ITailAppendable)tableRoute;
8991
//一定要先在路由里面添加尾巴
9092
virtualTableRoute.Append("20220921");
9193
shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
@@ -330,6 +332,13 @@ public async Task<IActionResult> Get9()
330332
[HttpGet]
331333
public async Task<IActionResult> Get10()
332334
{
335+
var shardingDbContextExecutor = _defaultTableDbContext.GetShardingExecutor();
336+
var dataSourceDbContexts = shardingDbContextExecutor.GetCurrentDbContexts();
337+
var sourceDbContexts = dataSourceDbContexts.Values;
338+
var dataSourceDbContext = sourceDbContexts.FirstOrDefault();
339+
var dbConnection = dataSourceDbContext.GetCurrentContexts().FirstOrDefault().Value.Database.GetDbConnection();
340+
341+
333342
var sysUserMod1 = await _defaultTableDbContext.Set<SysTest>().Where(o=>o.UserId=="11231").AllAsync(o=>o.Id=="1123"&&string.Compare(o.UserId,"123")>0);
334343
var sysUserMod2 = await _defaultTableDbContext.Set<SysTest>().AllAsync(o=>o.Id=="1123");
335344
var dateTime = new DateTime(2020,1,1);
@@ -354,5 +363,25 @@ await _defaultTableDbContext.Set<SysTest>().Where(o => o.Id == "11")
354363

355364
return Ok();
356365
}
366+
[HttpGet]
367+
public async Task<IActionResult> Get12()
368+
{
369+
var sysTests = from ut in _defaultTableDbContext.Set<SysTest>()
370+
where _defaultTableDbContext.Set<SysUserLogByMonth>().Any(x => x.Id == ut.Id)
371+
select ut;
372+
var tests = sysTests.ToList();
373+
return Ok();
374+
}
375+
[HttpGet]
376+
public async Task<IActionResult> Get13()
377+
{
378+
var list =await _defaultTableDbContext.Set<SysUserLogByMonth>()
379+
.GroupBy(o=>o.Time)
380+
.Select(o=>new
381+
{
382+
o.Key
383+
}).ToListAsync();
384+
return Ok();
385+
}
357386
}
358387
}

samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ namespace Sample.MySql.Domain.Entities
99
public class SysUserLogByMonth
1010
{
1111
public string Id { get; set; }
12-
public DateTime Time { get; set; }
12+
public DateTime? Time { get; set; }
1313
}
1414
}

samples/Sample.MySql/Domain/Maps/SysTestMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore.Metadata.Builders;
3+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
34
using Sample.MySql.Domain.Entities;
45

56
namespace Sample.MySql.Domain.Maps
@@ -12,7 +13,7 @@ public void Configure(EntityTypeBuilder<SysTest> builder)
1213
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
1314
builder.Property(o => o.UserId).IsRequired().HasMaxLength(128);
1415
builder.Property(o => o.UserId).IsConcurrencyToken();
15-
builder.ToTable(nameof(SysTest));
16+
builder.ToTable(nameof(SysTest),"dbdbd0");
1617
}
1718
}
1819
}

samples/Sample.MySql/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public void ConfigureServices(IServiceCollection services)
7474
services.AddShardingDbContext<DefaultShardingDbContext>()
7575
.UseRouteConfig((sp, o) =>
7676
{
77+
78+
// AppDomain.CurrentDomain.GetAssemblies().
7779
o.AddShardingTableRoute<DynamicTableRoute>();
7880
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
7981
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
@@ -93,6 +95,7 @@ public void ConfigureServices(IServiceCollection services)
9395
// {
9496
// b.UseMemoryCache(memoryCache);
9597
// });
98+
o.CheckShardingKeyValueGenerated = false;
9699
o.IgnoreCreateTableError = false;
97100
o.UseEntityFrameworkCoreProxies = true;
98101
o.CacheModelLockConcurrencyLevel = 1024;
@@ -107,6 +110,8 @@ public void ConfigureServices(IServiceCollection services)
107110

108111
o.UseShardingQuery((conStr, builder) =>
109112
{
113+
var logger = sp.ApplicationServiceProvider.GetService<ILogger<Startup>>();
114+
logger.LogInformation(conStr);
110115
builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
111116
.UseLoggerFactory(loggerFactory1)
112117
.EnableSensitiveDataLogging();
@@ -180,7 +185,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
180185
{
181186
app.UseDeveloperExceptionPage();
182187
}
183-
// app.ApplicationServices.UseAutoTryCompensateTable();
188+
app.ApplicationServices.UseAutoTryCompensateTable();
184189

185190
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
186191
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();

src/ShardingCore/Sharding/Enumerators/AggregateExtensions/AggregateExtension.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using System.Linq.Expressions;
@@ -20,16 +21,45 @@ namespace ShardingCore.Sharding.Enumerators.AggregateExtensions
2021
/// </summary>
2122
internal static class AggregateExtension
2223
{
24+
25+
public static object ChangeType(this object value, Type targetType)
26+
{
27+
//targetType类型是否为泛型,因为nullable是泛型类,
28+
if (targetType.IsGenericType &&
29+
//判断targetType是否为nullable泛型类
30+
targetType.IsNullableType())
31+
{
32+
if (value == null)
33+
{
34+
return null;
35+
}
36+
37+
NullableConverter nullableConverter = new NullableConverter(targetType);
38+
targetType = nullableConverter.UnderlyingType;
39+
}
40+
return Convert.ChangeType(value, targetType);
41+
}
2342
public static TSource CopyTSource<TSource>(TSource source)
2443
{
2544
var anonType = source.GetType();
2645
var allProperties = anonType.GetProperties();
2746
var allPropertyTypes= allProperties.Select(o=>o.PropertyType).ToArray();
2847
if (anonType.IsAnonymousType())
2948
{
30-
var constantExpressions = allProperties.Select(o => Expression.Constant(o.GetValue(source))).ToArray();
49+
var constantExpressions = allProperties.Select(o =>
50+
{
51+
var value = o.GetValue(source);
52+
if (value != null &&o.PropertyType.IsNullableType())
53+
{
54+
// var changeTypeValue = ChangeType(value,o.PropertyType);
55+
return Expression.Constant(value,o.PropertyType);
56+
}
57+
58+
return Expression.Constant(value);
59+
}).ToArray();
60+
var constructorInfo = anonType.GetConstructor(allPropertyTypes);
3161
var exp = Expression.New(
32-
anonType.GetConstructor(allPropertyTypes),
62+
constructorInfo,
3363
constantExpressions);
3464
var lambda = LambdaExpression.Lambda(exp);
3565
TSource myObj = (TSource)lambda.Compile().DynamicInvoke();

0 commit comments

Comments
 (0)