From 768745853b2ac5327a818cb64ae0d859d2703b90 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Tue, 30 Aug 2022 10:25:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=BB=98=E8=AE=A4=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E5=A4=9A=E4=B8=AA=E7=A7=BB=E9=99=A4=E9=9D=99?= =?UTF-8?q?=E6=80=81=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/Sample.MySql/Startup.cs | 5 ++++- samples/Sample.MySql/multi/MyUserRoute.cs | 2 ++ src/ShardingCore/Helpers/ShardingCoreHelper.cs | 8 +++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 06208d94..88231ecd 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -66,7 +66,10 @@ namespace Sample.MySql // op.AddShardingTableRoute(); // }); services.AddMultiShardingDbContext() - .UseRouteConfig(op => { op.AddShardingTableRoute(); }) + .UseRouteConfig(op => + { + op.AddShardingTableRoute(); + }) .UseConfig((sp,o) => { o.ThrowIfQueryRouteNotMatch = false; diff --git a/samples/Sample.MySql/multi/MyUserRoute.cs b/samples/Sample.MySql/multi/MyUserRoute.cs index cb71a695..a0f42234 100644 --- a/samples/Sample.MySql/multi/MyUserRoute.cs +++ b/samples/Sample.MySql/multi/MyUserRoute.cs @@ -5,6 +5,8 @@ namespace Sample.MySql.multi; public class MyUserRoute:AbstractSimpleShardingModKeyStringVirtualTableRoute { + protected override bool EnableHintRoute => true; + public MyUserRoute() : base(1, 3) { } diff --git a/src/ShardingCore/Helpers/ShardingCoreHelper.cs b/src/ShardingCore/Helpers/ShardingCoreHelper.cs index 11f4417f..aaa55a17 100644 --- a/src/ShardingCore/Helpers/ShardingCoreHelper.cs +++ b/src/ShardingCore/Helpers/ShardingCoreHelper.cs @@ -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)) { throw new ArgumentException($"dbcontext : {contextType} declared constructor parameters should use {typeof(ShardingDbContextOptions)} or {typeof(DbContextOptions)} or {typeof(DbContextOptions)},if u want support multi constructor params plz replace ${nameof(IDbContextCreator)} interface ");