From 674350b60d3f1415907ef476566d6f892a6e8c26 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Sun, 13 Nov 2022 13:20:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9demo=E5=88=86=E8=A1=A8?= =?UTF-8?q?=E5=88=86=E5=BA=93=E5=92=8Ccode=5Ffirst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/Sample.AutoCreateIfPresent/AreaDevice.cs | 1 - samples/Sample.MySql/Startup.cs | 8 ++++---- samples/Sample.MySql/appsettings.Development.json | 4 ++-- .../Controllers/ValuesController.cs | 2 +- samples/Sample.SqlServerShardingAll/Startup.cs | 14 +++++++++++--- .../Sample.SqlServerShardingDataSource/Startup.cs | 13 +++++++++++-- samples/Sample.SqlServerShardingTable/Startup.cs | 1 + 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/samples/Sample.AutoCreateIfPresent/AreaDevice.cs b/samples/Sample.AutoCreateIfPresent/AreaDevice.cs index ce02a04b..961e686a 100644 --- a/samples/Sample.AutoCreateIfPresent/AreaDevice.cs +++ b/samples/Sample.AutoCreateIfPresent/AreaDevice.cs @@ -2,7 +2,6 @@ namespace Sample.AutoCreateIfPresent { - [Table("aaa")] public class AreaDevice { public string Id { get; set; } diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index dfa04e99..e69c994f 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -109,16 +109,16 @@ namespace Sample.MySql o.UseShardingQuery((conStr, builder) => { builder.UseMySql(conStr, new MySqlServerVersion(new Version())) - .UseLoggerFactory(efLogger) - .EnableSensitiveDataLogging() + // .UseLoggerFactory(efLogger) + // .EnableSensitiveDataLogging() .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.UseShardingTransaction((connection, builder) => { builder .UseMySql(connection, new MySqlServerVersion(new Version())) - .UseLoggerFactory(efLogger) - .EnableSensitiveDataLogging() + // .UseLoggerFactory(efLogger) + // .EnableSensitiveDataLogging() .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); o.AddDefaultDataSource("ds0", diff --git a/samples/Sample.MySql/appsettings.Development.json b/samples/Sample.MySql/appsettings.Development.json index 34b9d208..63ed6e14 100644 --- a/samples/Sample.MySql/appsettings.Development.json +++ b/samples/Sample.MySql/appsettings.Development.json @@ -1,8 +1,8 @@ { "Logging": { "LogLevel": { - "Default": "Debug", - "Microsoft": "Debug", + "Default": "Information", + "Microsoft": "Information", "Microsoft.Hosting.Lifetime": "Information" } } diff --git a/samples/Sample.SqlServer/Controllers/ValuesController.cs b/samples/Sample.SqlServer/Controllers/ValuesController.cs index c9effb28..b0d4306d 100644 --- a/samples/Sample.SqlServer/Controllers/ValuesController.cs +++ b/samples/Sample.SqlServer/Controllers/ValuesController.cs @@ -176,7 +176,7 @@ namespace Sample.SqlServer.Controllers // var provider = queryable.Provider as EntityQueryProvider; // var compiler = provider.GetFieldValue("_queryCompiler") as ShardingQueryCompiler; // var shardingDbContext = compiler.GetFieldValue("_shardingDbContext") as IShardingDbContext; - + Stopwatch sp = new Stopwatch(); sp.Start(); var shardingPageResultAsync = await _defaultTableDbContext.Set() diff --git a/samples/Sample.SqlServerShardingAll/Startup.cs b/samples/Sample.SqlServerShardingAll/Startup.cs index 959d1d54..806c0528 100644 --- a/samples/Sample.SqlServerShardingAll/Startup.cs +++ b/samples/Sample.SqlServerShardingAll/Startup.cs @@ -65,7 +65,7 @@ namespace Sample.SqlServerShardingAll }, }; }); - }).ReplaceService().AddShardingCore(); + }).AddShardingCore(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -75,8 +75,16 @@ namespace Sample.SqlServerShardingAll { app.UseDeveloperExceptionPage(); } - - app.UseShardingCore(); + //建议补偿表在迁移后面 + using (var scope = app.ApplicationServices.CreateScope()) + { + var myDbContext = scope.ServiceProvider.GetService(); + //如果没有迁移那么就直接创建表和库 + myDbContext.Database.EnsureCreated(); + //如果有迁移使用下面的 + // myDbContext.Database.Migrate(); + } + app.ApplicationServices.UseAutoTryCompensateTable(); app.UseRouting(); app.UseAuthorization(); diff --git a/samples/Sample.SqlServerShardingDataSource/Startup.cs b/samples/Sample.SqlServerShardingDataSource/Startup.cs index acaa14d2..3732cb43 100644 --- a/samples/Sample.SqlServerShardingDataSource/Startup.cs +++ b/samples/Sample.SqlServerShardingDataSource/Startup.cs @@ -60,7 +60,7 @@ namespace Sample.SqlServerShardingDataSource o => $"Data Source=localhost;Initial Catalog=EFCoreShardingDataSourceOnly{o};Integrated Security=True;"); }); - }).ReplaceService().AddShardingCore(); + }).AddShardingCore(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -71,7 +71,16 @@ namespace Sample.SqlServerShardingDataSource app.UseDeveloperExceptionPage(); } - app.UseShardingCore(); + //建议补偿表在迁移后面 + using (var scope = app.ApplicationServices.CreateScope()) + { + var myDbContext = scope.ServiceProvider.GetService(); + //如果没有迁移那么就直接创建表和库 + myDbContext.Database.EnsureCreated(); + //如果有迁移使用下面的 + // myDbContext.Database.Migrate(); + } + app.ApplicationServices.UseAutoTryCompensateTable(); app.UseRouting(); app.UseAuthorization(); diff --git a/samples/Sample.SqlServerShardingTable/Startup.cs b/samples/Sample.SqlServerShardingTable/Startup.cs index ec7e412e..2e75d03b 100644 --- a/samples/Sample.SqlServerShardingTable/Startup.cs +++ b/samples/Sample.SqlServerShardingTable/Startup.cs @@ -113,6 +113,7 @@ namespace Sample.SqlServerShardingTable app.UseDeveloperExceptionPage(); } + //建议补偿表在迁移后面 using (var scope = app.ApplicationServices.CreateScope()) { var myDbContext = scope.ServiceProvider.GetService();