diff --git a/README-zh.md b/README-zh.md index 641d6678..04559ec1 100644 --- a/README-zh.md +++ b/README-zh.md @@ -1,5 +1,5 @@

- +

# ShardingCore diff --git a/README.md b/README.md index 253aa8fb..059de21c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- +

# ShardingCore diff --git a/nuget-publish.bat b/nuget-publish.bat index 01bb7bec..a245d294 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,9 +1,9 @@ :start ::定义版本 -set EFCORE2=2.4.0.02 -set EFCORE3=3.4.0.02 -set EFCORE5=5.4.0.02 -set EFCORE6=6.4.0.02 +set EFCORE2=2.4.0.03 +set EFCORE3=3.4.0.03 +set EFCORE5=5.4.0.03 +set EFCORE6=6.4.0.03 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs index 507366d9..e6290fbb 100644 --- a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs @@ -11,6 +11,8 @@ namespace Sample.MySql.DbContexts { public DefaultShardingDbContext(DbContextOptions options) : base(options) { + //切记不要在构造函数中使用会让模型提前创建的方法 + //ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/samples/Sample.MySql/Properties/launchSettings.json b/samples/Sample.MySql/Properties/launchSettings.json index 82b6a434..592f72a5 100644 --- a/samples/Sample.MySql/Properties/launchSettings.json +++ b/samples/Sample.MySql/Properties/launchSettings.json @@ -9,14 +9,6 @@ } }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "weatherforecast", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, "Sample.MySql": { "commandName": "Project", "launchBrowser": true, diff --git a/samples/Sample.MySql/Sample.MySql.csproj b/samples/Sample.MySql/Sample.MySql.csproj index 879cd8b0..ad1f7c4f 100644 --- a/samples/Sample.MySql/Sample.MySql.csproj +++ b/samples/Sample.MySql/Sample.MySql.csproj @@ -1,11 +1,17 @@  - netcoreapp3.1 + net6.0 + enable + enable - + + + + + diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index ed6020b8..39ccde46 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -1,14 +1,23 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Sample.MySql.DbContexts; +using Sample.MySql.Shardings; +using ShardingCore; +using ShardingCore.TableExists; namespace Sample.MySql { 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 Startup(IConfiguration configuration) { Configuration = configuration; @@ -30,6 +39,33 @@ namespace Sample.MySql // op.AddShardingTableRoute(); // op.AddShardingTableRoute(); // }); + + services.AddShardingDbContext(ServiceLifetime.Transient, ServiceLifetime.Transient) + .AddEntityConfig(o => + { + o.CreateShardingTableOnStart = true; + o.EnsureCreatedWithOutShardingTable = true; + o.AddShardingTableRoute(); + o.AddShardingTableRoute(); + o.UseShardingQuery((conStr, builder) => + { + builder.UseMySql(conStr, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger); + //builder.UseMySql(conStr, new MySqlServerVersion(new Version())); + }); + o.UseShardingTransaction((connection, builder) => + { + builder.UseMySql(connection, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger); + }); + }) + .AddConfig(op => + { + op.ConfigId = "c1"; + op.AddDefaultDataSource("ds0", + "server=127.0.0.1;port=3306;database=dbxxxx;userid=root;password=L6yBtV6qNENrwBy7;"); + + //op.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=db2;userid=root;password=L6yBtV6qNENrwBy7;") + op.ReplaceTableEnsureManager(sp=>new MySqlTableEnsureManager()); + }).EnsureConfig(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/samples/Sample.SqlServer/DIExtension.cs b/samples/Sample.SqlServer/DIExtension.cs index 7c11cdac..8b881388 100644 --- a/samples/Sample.SqlServer/DIExtension.cs +++ b/samples/Sample.SqlServer/DIExtension.cs @@ -31,7 +31,7 @@ namespace Sample.SqlServer using (var scope = app.ApplicationServices.CreateScope()) { var virtualDbContext = scope.ServiceProvider.GetService(); - if (!virtualDbContext.Set().Any()) + if (!virtualDbContext.Set().Any(o=>o.Id=="111")) { var ids = Enumerable.Range(1, 1000); var userMods = new List(); diff --git a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs index 46af3262..bb9e5ea0 100644 --- a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs +++ b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs @@ -131,52 +131,102 @@ namespace ShardingCore.Core.Internal.Visitors private object GetShardingKeyValue(Expression expression) { - if (expression is ConstantExpression constantExpression) + if (expression == null) + return null; + switch (expression) { - return constantExpression.Value; - } - if (expression is UnaryExpression unaryExpression) - { - return Expression.Lambda(unaryExpression.Operand).Compile().DynamicInvoke(); - } + case ConstantExpression e: + return e.Value; - if (expression is MemberExpression member1Expression) - { + case MemberExpression e when e.Member is FieldInfo field: + return field.GetValue( + GetShardingKeyValue( + e.Expression + ) + ); - if (member1Expression.Expression is ConstantExpression memberConstantExpression) - { - if (member1Expression.Member is FieldInfo memberFieldInfo) + case MemberExpression e when e.Member is PropertyInfo property: + return property.GetValue( + GetShardingKeyValue( + e.Expression + ) + ); + + case ListInitExpression e when e.NewExpression.Arguments.Count() == 0: + var collection = e.NewExpression.Constructor.Invoke(new object[0]); + foreach (var i in e.Initializers) { - object container = memberConstantExpression.Value; - return memberFieldInfo.GetValue(container); + i.AddMethod.Invoke( + collection, + i.Arguments + .Select( + a => GetShardingKeyValue(a) + ) + .ToArray() + ); } - if (member1Expression.Member is PropertyInfo memberPropertyInfo) - { - object container = memberConstantExpression.Value; - return memberPropertyInfo.GetValue(container); - } - else - { - return memberConstantExpression.Value; - } - } - return Expression.Lambda(member1Expression).Compile().DynamicInvoke(); - } + return collection; - if (expression is MethodCallExpression methodCallExpression) - { - return Expression.Lambda(methodCallExpression).Compile().DynamicInvoke(); - //return methodCallExpression.Method.Invoke( - // GetShardingKeyValue(methodCallExpression.Object), - // methodCallExpression.Arguments - // .Select( - // a => GetShardingKeyValue(a) - // ) - // .ToArray() - //); - } + case MethodCallExpression e: + return e.Method.Invoke( + GetShardingKeyValue(e.Object), + e.Arguments + .Select( + a => GetShardingKeyValue(a) + ) + .ToArray() + ); - throw new ShardingCoreException("cant get value " + expression); + default: + //TODO: better messaging + throw new ShardingCoreException("cant get value " + expression); + } + //if (expression is ConstantExpression constantExpression) + //{ + // return constantExpression.Value; + //} + //if (expression is UnaryExpression unaryExpression) + //{ + // return Expression.Lambda(unaryExpression.Operand).Compile().DynamicInvoke(); + //} + + //if (expression is MemberExpression member1Expression) + //{ + + // if (member1Expression.Expression is ConstantExpression memberConstantExpression) + // { + // if (member1Expression.Member is FieldInfo memberFieldInfo) + // { + // object container = memberConstantExpression.Value; + // return memberFieldInfo.GetValue(container); + // } + // if (member1Expression.Member is PropertyInfo memberPropertyInfo) + // { + // object container = memberConstantExpression.Value; + // return memberPropertyInfo.GetValue(container); + // } + // else + // { + // return memberConstantExpression.Value; + // } + // } + // return Expression.Lambda(member1Expression).Compile().DynamicInvoke(); + //} + + //if (expression is MethodCallExpression methodCallExpression) + //{ + // return Expression.Lambda(methodCallExpression).Compile().DynamicInvoke(); + // //return methodCallExpression.Method.Invoke( + // // GetShardingKeyValue(methodCallExpression.Object), + // // methodCallExpression.Arguments + // // .Select( + // // a => GetShardingKeyValue(a) + // // ) + // // .ToArray() + // //); + //} + + //throw new ShardingCoreException("cant get value " + expression); } protected override Expression VisitMethodCall(MethodCallExpression node) diff --git a/src/ShardingCore/Utils/ShardingUtil.cs b/src/ShardingCore/Utils/ShardingUtil.cs index ed6d18bb..a06ca9b0 100644 --- a/src/ShardingCore/Utils/ShardingUtil.cs +++ b/src/ShardingCore/Utils/ShardingUtil.cs @@ -47,7 +47,6 @@ namespace ShardingCore.Utils /// /// ֱ·ɹ /// - /// /// /// ///