mysql sample

This commit is contained in:
xuejmnet 2021-08-23 19:30:52 +08:00
parent 87f60729f4
commit 635b509811
3 changed files with 40 additions and 31 deletions

View File

@ -0,0 +1,24 @@
using System;
using Microsoft.EntityFrameworkCore;
using Sample.MySql.Domain.Maps;
using ShardingCore.Sharding;
namespace Sample.MySql.DbContexts
{
public class DefaultShardingDbContext:AbstractShardingDbContext<DefaultTableDbContext>
{
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new SysUserModMap());
modelBuilder.ApplyConfiguration(new SysTestMap());
modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap());
}
public override Type ShardingDbContextType => this.GetType();
}
}

View File

@ -20,24 +20,16 @@ namespace Sample.MySql
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddControllers(); services.AddControllers();
// services.AddShardingMySql(o => // services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// { // ,op =>
// o.EnsureCreatedWithOutShardingTable = true;
// o.CreateShardingTableOnStart = true;
// o.UseShardingDbContext<DefaultTableDbContext>( dbConfig =>
// { // {
// dbConfig.AddShardingTableRoute<SysUserModVirtualTableRoute>(); // op.EnsureCreatedWithOutShardingTable = true;
// dbConfig.AddShardingTableRoute<SysUserLogByMonthRoute>(); // op.CreateShardingTableOnStart = true;
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// }); // });
// //o.AddDataSourceVirtualRoute<>();
// o.IgnoreCreateTableError = true;
//
// });
// services.AddDbContext<DefaultTableDbContext>(o => o.UseMySql("server=xxx;userid=xxx;password=xxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", o =>
// {
// o.ServerVersion("5.7.13");
// }).UseShardingMySqlUpdateSqlGenerator());
// services.AddLogging(b => b.AddConsole());
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -38,23 +38,16 @@ namespace ShardingCore.Test50.MySql
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
{ {
// services.AddShardingMySql(o => // services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// { // ,op =>
// o.EnsureCreatedWithOutShardingTable = false;
// o.CreateShardingTableOnStart = false;
// // o.AddShardingTable<DefaultDbContext>("conn1", "server=xxx;userid=xxx;password=xxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig =>
// o.UseShardingDbContext<DefaultDbContext>( dbConfig =>
// { // {
// dbConfig.AddShardingTableRoute<SysUserModVirtualTableRoute>(); // op.EnsureCreatedWithOutShardingTable = true;
// dbConfig.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>(); // op.CreateShardingTableOnStart = true;
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
// }); // });
// //o.AddDataSourceVirtualRoute<>();
// o.IgnoreCreateTableError = true;
// o.ServerVersion = new MySqlServerVersion("5.7.15");
// });
// services.AddDbContext<DefaultDbContext>(o=>
// o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
// .UseShardingMySqlUpdateSqlGenerator());
} }
// 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法