修复默认判断构造函数是否存在多个移除静态构造函数的判断

This commit is contained in:
xuejiaming 2022-08-30 10:25:01 +08:00
parent 7837e78e55
commit 768745853b
3 changed files with 11 additions and 4 deletions

View File

@ -66,7 +66,10 @@ namespace Sample.MySql
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// });
services.AddMultiShardingDbContext<OtherDbContext>()
.UseRouteConfig(op => { op.AddShardingTableRoute<MyUserRoute>(); })
.UseRouteConfig(op =>
{
op.AddShardingTableRoute<MyUserRoute>();
})
.UseConfig((sp,o) =>
{
o.ThrowIfQueryRouteNotMatch = false;

View File

@ -5,6 +5,8 @@ namespace Sample.MySql.multi;
public class MyUserRoute:AbstractSimpleShardingModKeyStringVirtualTableRoute<MyUser>
{
protected override bool EnableHintRoute => true;
public MyUserRoute() : base(1, 3)
{
}

View File

@ -90,16 +90,18 @@ namespace ShardingCore.Helpers
{
var contextType = typeof(TContext);
var declaredConstructors = contextType.GetTypeInfo().DeclaredConstructors.ToList();
if (declaredConstructors.Count != 1)
if (declaredConstructors.Count(o => !o.IsStatic) != 1)
{
throw new ArgumentException($"dbcontext : {contextType} declared constructor count more {contextType},if u want support multi constructor params plz replace ${nameof(IDbContextCreator)} interface");
}
if (declaredConstructors[0].GetParameters().Length != 1)
var defaultDeclaredConstructor = declaredConstructors.First(o=>!o.IsStatic);
if (defaultDeclaredConstructor.GetParameters().Length != 1)
{
throw new ArgumentException($"dbcontext : {contextType} declared constructor parameters more ,if u want support multi constructor params plz replace ${nameof(IDbContextCreator)} interface");
}
var paramType = declaredConstructors[0].GetParameters()[0].ParameterType;
var paramType = defaultDeclaredConstructor.GetParameters()[0].ParameterType;
if (paramType != typeof(ShardingDbContextOptions) && paramType != typeof(DbContextOptions) && paramType != typeof(DbContextOptions<TContext>))
{
throw new ArgumentException($"dbcontext : {contextType} declared constructor parameters should use {typeof(ShardingDbContextOptions)} or {typeof(DbContextOptions)} or {typeof(DbContextOptions<TContext>)},if u want support multi constructor params plz replace ${nameof(IDbContextCreator)} interface ");