添加!contains代码和优化表达式null判断

This commit is contained in:
xuejiaming 2022-05-19 09:38:32 +08:00
parent c1fc5cd404
commit f6e669a442
3 changed files with 13 additions and 8 deletions

View File

@ -25,10 +25,10 @@ namespace Sample.SqlServer
}
public class Startup
{
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
//public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
//{
// builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
//});
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
@ -51,7 +51,8 @@ namespace Sample.SqlServer
op.MaxQueryConnectionsLimit = 5;
op.UseSqlServer(builder =>
{
builder.UseLoggerFactory(efLogger).UseUnionAllMerge<DefaultShardingDbContext>();
var loggerFactory = ShardingContainer.GetService<ILoggerFactory>();
builder.UseLoggerFactory(loggerFactory).UseUnionAllMerge<DefaultShardingDbContext>();
});
op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<DefaultShardingDbContext>());
op.AddDefaultDataSource("A",

View File

@ -75,7 +75,7 @@ namespace ShardingCore.Core.Internal.Visitors
{
if (expression is MemberExpression member)
{
if (member.Expression.Type == _entityMetadata.EntityType|| MemberExpressionIsConvertAndOriginalIsEntityType(member))
if (member.Expression?.Type == _entityMetadata.EntityType|| MemberExpressionIsConvertAndOriginalIsEntityType(member))
{
var isShardingKey = false;
if (_shardingTableRoute)
@ -89,7 +89,7 @@ namespace ShardingCore.Core.Internal.Visitors
if (isShardingKey)
{
shardingPredicateResult = new ShardingPredicateResult(isShardingKey, isShardingKey ? member.Member.Name : null);
shardingPredicateResult = new ShardingPredicateResult(true, member.Member.Name);
return true;
}
}
@ -105,7 +105,7 @@ namespace ShardingCore.Core.Internal.Visitors
/// <returns></returns>
private bool MemberExpressionIsConvertAndOriginalIsEntityType(MemberExpression member)
{
return member.Expression.NodeType == ExpressionType.Convert &&
return member.Expression?.NodeType == ExpressionType.Convert &&
member.Expression is UnaryExpression unaryExpression &&
unaryExpression.Operand.Type == _entityMetadata.EntityType;
}

View File

@ -247,9 +247,13 @@ namespace ShardingCore.Test
var multiOrder = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 232398109278351360).FirstOrDefaultAsync();
Assert.NotNull(multiOrder);
var allMultiOrders = await _virtualDbContext.Set<MultiShardingOrder>().ToListAsync();
Assert.Equal(8, allMultiOrders.Count);
var longs = new[] { 232398109278351360, 255197859283087360 };
var multiOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => longs.Contains(o.Id)).ToListAsync();
Assert.Equal(2, multiOrders.Count);
var multinNotOrders = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => !longs.Contains(o.Id)).ToListAsync();
Assert.Equal(6, multinNotOrders.Count);
var dateTime = new DateTime(2021, 11, 1);
var multiOrder404 = await _virtualDbContext.Set<MultiShardingOrder>().Where(o => o.Id == 250345338962063360 && o.CreateTime < dateTime).FirstOrDefaultAsync();
Assert.Null(multiOrder404);