修改demo分表分库和code_first

This commit is contained in:
xuejiaming 2022-11-13 13:20:39 +08:00
parent f3cb2b7626
commit 674350b60d
7 changed files with 30 additions and 13 deletions

View File

@ -2,7 +2,6 @@
namespace Sample.AutoCreateIfPresent
{
[Table("aaa")]
public class AreaDevice
{
public string Id { get; set; }

View File

@ -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",

View File

@ -1,8 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Debug",
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}

View File

@ -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<SysUserMod>()

View File

@ -65,7 +65,7 @@ namespace Sample.SqlServerShardingAll
},
};
});
}).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>().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>();
//如果没有迁移那么就直接创建表和库
myDbContext.Database.EnsureCreated();
//如果有迁移使用下面的
// myDbContext.Database.Migrate();
}
app.ApplicationServices.UseAutoTryCompensateTable();
app.UseRouting();
app.UseAuthorization();

View File

@ -60,7 +60,7 @@ namespace Sample.SqlServerShardingDataSource
o =>
$"Data Source=localhost;Initial Catalog=EFCoreShardingDataSourceOnly{o};Integrated Security=True;");
});
}).ReplaceService<ITableEnsureManager,SqlServerTableEnsureManager>().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>();
//如果没有迁移那么就直接创建表和库
myDbContext.Database.EnsureCreated();
//如果有迁移使用下面的
// myDbContext.Database.Migrate();
}
app.ApplicationServices.UseAutoTryCompensateTable();
app.UseRouting();
app.UseAuthorization();

View File

@ -113,6 +113,7 @@ namespace Sample.SqlServerShardingTable
app.UseDeveloperExceptionPage();
}
//建议补偿表在迁移后面
using (var scope = app.ApplicationServices.CreateScope())
{
var myDbContext = scope.ServiceProvider.GetService<MyDbContext>();