diff --git a/README-zh.md b/README-zh.md index b27c2629..08555a6c 100644 --- a/README-zh.md +++ b/README-zh.md @@ -12,6 +12,7 @@ --- - [Gitee](https://gitee.com/dotnetchina/sharding-core) 国内镜像 +- [捐赠](#捐赠) ## 社区合作伙伴和赞助商 @@ -1085,6 +1086,10 @@ return optionsBuilder.ReplaceService + + [博客](https://www.cnblogs.com/xuejiaming) QQ群:771630778 diff --git a/README.md b/README.md index 49008470..f1e1fc8f 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ high performance lightweight solution for efcore sharding table and sharding dat --- - [Gitee](https://gitee.com/dotnetchina/sharding-core) 国内镜像 +- [捐赠](#捐赠) ## Community Partners and Sponsors @@ -1101,6 +1102,9 @@ return optionsBuilder.ReplaceService + [博客](https://www.cnblogs.com/xuejiaming) QQ群:771630778 diff --git a/imgs/wx.jpg b/imgs/wx.jpg new file mode 100644 index 00000000..f6921079 Binary files /dev/null and b/imgs/wx.jpg differ diff --git a/imgs/zfb.jpg b/imgs/zfb.jpg new file mode 100644 index 00000000..6f33a40f Binary files /dev/null and b/imgs/zfb.jpg differ diff --git a/samples/Sample.MySql/Controllers/DbSetDiscoverExpressionVisitor.cs b/samples/Sample.MySql/Controllers/DbSetDiscoverExpressionVisitor.cs new file mode 100644 index 00000000..fed700fe --- /dev/null +++ b/samples/Sample.MySql/Controllers/DbSetDiscoverExpressionVisitor.cs @@ -0,0 +1,31 @@ +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Internal; +using Microsoft.EntityFrameworkCore.Query; +using ShardingCore.Extensions; + +namespace Sample.MySql.Controllers; + +public class DbSetDiscoverExpressionVisitor:ExpressionVisitor where TEntity:class +{ + private readonly DbContext _dbContext; + public DbSet DbSet { get; private set; } + + public DbSetDiscoverExpressionVisitor(DbContext dbContext) + { + _dbContext = dbContext; + } + + protected override Expression VisitExtension(Expression node) + { + if (node is QueryRootExpression queryRootExpression) + { + var dbContextDependencies = + typeof(DbContext).GetTypePropertyValue(_dbContext, "DbContextDependencies") as IDbContextDependencies; + var targetIQ = + ((IDbSetCache)_dbContext).GetOrAddSet(dbContextDependencies.SetSource, queryRootExpression.EntityType.ClrType); + DbSet = (DbSet)targetIQ; + } + return base.VisitExtension(node); + } +} \ No newline at end of file diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index b71633cc..b8d4809d 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -32,6 +32,18 @@ namespace Sample.MySql.Controllers [HttpGet] public async Task Get() { + OtherDbContext.CurrentId = ""; + var myUsers0 = _otherDbContext.MyUsers.ToList(); + OtherDbContext.CurrentId = "123"; + var myUsers1 = _otherDbContext.MyUsers.ToList(); + OtherDbContext.CurrentId = "456"; + var myUsers2= _otherDbContext.MyUsers.ToList(); + + // var sysUserModQueryable = _otherDbContext.MyUsers.Where(o => o.Id == "2"); + // var dbSetDiscoverExpressionVisitor = new DbSetDiscoverExpressionVisitor(_otherDbContext); + // dbSetDiscoverExpressionVisitor.Visit(sysUserModQueryable.Expression); + // var myUsers = dbSetDiscoverExpressionVisitor.DbSet; + // Console.WriteLine("------------"); // using (var tran = _defaultTableDbContext.Database.BeginTransaction()) // { diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 2378f2d4..6da875b1 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -74,6 +74,7 @@ namespace Sample.MySql { builder.UseMySql(conStr, new MySqlServerVersion(new Version())) .UseLoggerFactory(efLogger) + .EnableSensitiveDataLogging() .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.UseShardingTransaction((connection, builder) => @@ -81,6 +82,7 @@ namespace Sample.MySql builder .UseMySql(connection, new MySqlServerVersion(new Version())) .UseLoggerFactory(efLogger) + .EnableSensitiveDataLogging() .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.UseShardingMigrationConfigure(b => @@ -107,6 +109,7 @@ namespace Sample.MySql { builder.UseMySql(conStr, new MySqlServerVersion(new Version())) .UseLoggerFactory(efLogger) + .EnableSensitiveDataLogging() .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.UseShardingTransaction((connection, builder) => @@ -114,6 +117,7 @@ namespace Sample.MySql builder .UseMySql(connection, new MySqlServerVersion(new Version())) .UseLoggerFactory(efLogger) + .EnableSensitiveDataLogging() .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.AddDefaultDataSource("ds0", @@ -123,20 +127,6 @@ namespace Sample.MySql { "ds1", "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" }, { "ds2", "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;" } }); - // o.AddReadWriteSeparation(sp => - // { - // return new Dictionary>() - // { - // { - // "ds0", - // new[] - // { - // "server=127.0.0.1;port=3306;database=dbdbd0_0;userid=root;password=root;" - // } - // } - // }; - // }, defaultEnable: true, readStrategyEnum: ReadStrategyEnum.Loop, - // readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); o.UseShardingMigrationConfigure(b => { b.ReplaceService(); diff --git a/samples/Sample.MySql/appsettings.Development.json b/samples/Sample.MySql/appsettings.Development.json index 4f30a00f..8983e0fc 100644 --- a/samples/Sample.MySql/appsettings.Development.json +++ b/samples/Sample.MySql/appsettings.Development.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Debug", + "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } diff --git a/samples/Sample.MySql/multi/OtherDbContext.cs b/samples/Sample.MySql/multi/OtherDbContext.cs index 4c8bb076..371a1b7b 100644 --- a/samples/Sample.MySql/multi/OtherDbContext.cs +++ b/samples/Sample.MySql/multi/OtherDbContext.cs @@ -12,5 +12,15 @@ public class OtherDbContext:AbstractShardingDbContext,IShardingTableDbContext { } + public static string CurrentId; + public string CID => CurrentId; + public bool HasCID => !string.IsNullOrWhiteSpace(CID); + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .HasQueryFilter(o => !HasCID || o.Id == CID); + } + public IRouteTail RouteTail { get; set; } } \ No newline at end of file