[#235] 支持linq where条件判断

This commit is contained in:
xuejiaming 2023-08-25 22:17:54 +08:00
parent 015c4d6c44
commit 284a991a6c
5 changed files with 51 additions and 1 deletions

View File

@ -21,5 +21,9 @@ namespace ShardingCore6x.ShardingDbContexts
builder.ShardingProperty(o => o.Id);
}
public override string CompareValueToKey(object shardingKey)
{
return shardingKey.ToString();
}
}
}

View File

@ -76,6 +76,11 @@ namespace Sample.MySql.Controllers
public IQueryable<SysTest> GetAll()
{
var dataSourceRouteManager = _shardingRuntimeContext.GetDataSourceRouteManager();
// dataSourceRouteManager.GetRoute()
var routeManager = _shardingRuntimeContext.GetTableRouteManager();
// routeManager.GetRoute()
// DynamicShardingHelper.DynamicAppendDataSource(_shardingRuntimeContext,"ds9","链接字符串",true,true);
//如果你已经添加好了的情况下并且没有生成对应的库和表想要生成表和库
var dataSourceInitializer = _shardingRuntimeContext.GetDataSourceInitializer();
@ -84,7 +89,7 @@ namespace Sample.MySql.Controllers
// _defaultTableDbContext.ReadWriteSeparationReadOnly();//读库
// _defaultTableDbContext.ReadWriteSeparationWriteOnly();//写库
var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
var tableRouteManager = routeManager;
//系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod));
var tails = tableRoute.GetTails();

View File

@ -13,6 +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.HasIndex(o => new { o.Id, o.UserId });
builder.ToTable(nameof(SysTest),"dbdbd0");
}
}

View File

@ -40,6 +40,25 @@ namespace Sample.MySql
// return Task.CompletedTask;
// }
// }
// public interface ITenant
// {
// string GetTenantId();
// }
//
// public class DefaultTenant :ITenant
// {
// private readonly string _tenantId;
//
// public DefaultTenant(string tenantId)
// {
// _tenantId = tenantId;
// }
// public string GetTenantId()
// {
// return _tenantId;
// }
// }
public class Startup
{
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
@ -69,6 +88,7 @@ namespace Sample.MySql
// optionsBuilder(sp, builder);
// });
//
var type = typeof(MyUserRoute);
services.AddDbContext<UnShardingDbContext>(o =>
o.UseMySql(

View File

@ -287,6 +287,26 @@ namespace ShardingCore.Core.Internal.Visitors
}
}
//todo
else if(lambdaExpression.Parameters[0].Type.IsGenericType&&lambdaExpression.Parameters[0].Type.IsAnonymousType())
{
var typeGenericTypeArguments = lambdaExpression.Parameters[0].Type.GenericTypeArguments;
foreach (var typeGenericTypeArgument in typeGenericTypeArguments)
{
if (typeGenericTypeArgument == _entityMetadata.EntityType)
{
if (_entityLambdaExpression == null)
{
_entityLambdaExpression = lambdaExpression;
}
else
{
var body = Expression.AndAlso(_entityLambdaExpression.Body, lambdaExpression.Body);
var lambda = Expression.Lambda(body, _entityLambdaExpression.Parameters[0]);
_entityLambdaExpression = lambda;
}
}
}
}
}
}
}