[#199]修复update-database指定migration

This commit is contained in:
xuejiaming 2022-10-14 10:38:30 +08:00
parent 07928d9cb2
commit 01b485e497
10 changed files with 478 additions and 24 deletions

View File

@ -1,6 +1,6 @@
:start
::定义版本
set SHARDINGCORE=6.7.0.3
set SHARDINGCORE=6.7.0.4
::删除所有bin与obj下的文件
@echo off

View File

@ -4,5 +4,6 @@
{
public string Id { get; set; }
public string UserId { get; set; }
public string Name { get; set; }
}
}

View File

@ -0,0 +1,114 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Sample.SqlServer.DbContexts;
#nullable disable
namespace Sample.SqlServer.Migrations
{
[DbContext(typeof(DefaultShardingDbContext))]
[Migration("20221014021906_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysTest", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysTest", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysUserMod", b =>
{
b.Property<string>("Id")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<int>("Age")
.HasColumnType("int");
b.Property<int>("AgeGroup")
.HasColumnType("int");
b.Property<string>("Name")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysUserMod", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysUserSalary", b =>
{
b.Property<string>("Id")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<int>("DateOfMonth")
.HasColumnType("int");
b.Property<int>("Salary")
.HasColumnType("int");
b.Property<decimal>("SalaryDecimal")
.HasColumnType("decimal(18,2)");
b.Property<double>("SalaryDouble")
.HasColumnType("float");
b.Property<float>("SalaryFloat")
.HasColumnType("real");
b.Property<long>("SalaryLong")
.HasColumnType("bigint");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysUserSalary", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.TestYearSharding", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");
b.Property<DateTime>("CreateTIme")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("TestYearSharding", (string)null);
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,84 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Sample.SqlServer.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SysTest",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
UserId = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SysTest", x => x.Id);
});
migrationBuilder.CreateTable(
name: "SysUserMod",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
Age = table.Column<int>(type: "int", nullable: false),
AgeGroup = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SysUserMod", x => x.Id);
});
migrationBuilder.CreateTable(
name: "SysUserSalary",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
UserId = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
DateOfMonth = table.Column<int>(type: "int", nullable: false),
Salary = table.Column<int>(type: "int", nullable: false),
SalaryLong = table.Column<long>(type: "bigint", nullable: false),
SalaryDecimal = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
SalaryDouble = table.Column<double>(type: "float", nullable: false),
SalaryFloat = table.Column<float>(type: "real", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SysUserSalary", x => x.Id);
});
migrationBuilder.CreateTable(
name: "TestYearSharding",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
CreateTIme = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_TestYearSharding", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "SysTest");
migrationBuilder.DropTable(
name: "SysUserMod");
migrationBuilder.DropTable(
name: "SysUserSalary");
migrationBuilder.DropTable(
name: "TestYearSharding");
}
}
}

View File

@ -0,0 +1,117 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Sample.SqlServer.DbContexts;
#nullable disable
namespace Sample.SqlServer.Migrations
{
[DbContext(typeof(DefaultShardingDbContext))]
[Migration("20221014021942_InitialCreateAddName")]
partial class InitialCreateAddName
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysTest", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysTest", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysUserMod", b =>
{
b.Property<string>("Id")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<int>("Age")
.HasColumnType("int");
b.Property<int>("AgeGroup")
.HasColumnType("int");
b.Property<string>("Name")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysUserMod", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysUserSalary", b =>
{
b.Property<string>("Id")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<int>("DateOfMonth")
.HasColumnType("int");
b.Property<int>("Salary")
.HasColumnType("int");
b.Property<decimal>("SalaryDecimal")
.HasColumnType("decimal(18,2)");
b.Property<double>("SalaryDouble")
.HasColumnType("float");
b.Property<float>("SalaryFloat")
.HasColumnType("real");
b.Property<long>("SalaryLong")
.HasColumnType("bigint");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysUserSalary", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.TestYearSharding", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");
b.Property<DateTime>("CreateTIme")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("TestYearSharding", (string)null);
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Sample.SqlServer.Migrations
{
public partial class InitialCreateAddName : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Name",
table: "SysTest",
type: "nvarchar(max)",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Name",
table: "SysTest");
}
}
}

View File

@ -0,0 +1,115 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Sample.SqlServer.DbContexts;
#nullable disable
namespace Sample.SqlServer.Migrations
{
[DbContext(typeof(DefaultShardingDbContext))]
partial class DefaultShardingDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysTest", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysTest", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysUserMod", b =>
{
b.Property<string>("Id")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<int>("Age")
.HasColumnType("int");
b.Property<int>("AgeGroup")
.HasColumnType("int");
b.Property<string>("Name")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysUserMod", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.SysUserSalary", b =>
{
b.Property<string>("Id")
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<int>("DateOfMonth")
.HasColumnType("int");
b.Property<int>("Salary")
.HasColumnType("int");
b.Property<decimal>("SalaryDecimal")
.HasColumnType("decimal(18,2)");
b.Property<double>("SalaryDouble")
.HasColumnType("float");
b.Property<float>("SalaryFloat")
.HasColumnType("real");
b.Property<long>("SalaryLong")
.HasColumnType("bigint");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.HasKey("Id");
b.ToTable("SysUserSalary", (string)null);
});
modelBuilder.Entity("Sample.SqlServer.Domain.Entities.TestYearSharding", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");
b.Property<DateTime>("CreateTIme")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("TestYearSharding", (string)null);
});
#pragma warning restore 612, 618
}
}
}

View File

@ -7,8 +7,12 @@
<ItemGroup>
<PackageReference Include="EFCore.BulkExtensions" Version="6.5.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="6.0.10" />
</ItemGroup>
<ItemGroup>

View File

@ -10,9 +10,14 @@ using ShardingCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
using ShardingCore.Extensions;
using ShardingCore.Helpers;
using ShardingCore.Sharding;
using ShardingCore.Sharding.ReadWriteConfigurations;
namespace Sample.SqlServer
@ -53,24 +58,6 @@ namespace Sample.SqlServer
op.AddDefaultDataSource("A",
"Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"
);
op.AddReadWriteNodeSeparation(sp =>new Dictionary<string, IEnumerable<ReadNode>>()
{
{"A",new List<ReadNode>()
{
new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A2","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A3","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A4","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A5","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A6","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("A1","Data Source=localhost;Initial Catalog=ShardingCoreDBXA;Integrated Security=True;"),
new ReadNode("X","Data Source=localhost;Initial Catalog=ShardingCoreDBXA123;Integrated Security=True;"),
}}
},ReadStrategyEnum.Loop);
}).AddServiceConfigure(s =>
{
s.AddSingleton<ILoggerFactory>(sp => LoggerFactory.Create(builder =>
@ -100,7 +87,6 @@ namespace Sample.SqlServer
// {
// }).End();
services.AddHealthChecks().AddDbContextCheck<DefaultShardingDbContext>();
//services.Replace(ServiceDescriptor.Singleton<IDbContextCreator<DefaultShardingDbContext>, ActivatorDbContextCreator<DefaultShardingDbContext>>());
//services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
// o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
@ -131,7 +117,15 @@ namespace Sample.SqlServer
{
app.UseDeveloperExceptionPage();
}
app.ApplicationServices.UseAutoShardingCreate();
using (var serviceScope = app.ApplicationServices.CreateScope())
{
var defaultShardingDbContext = serviceScope.ServiceProvider.GetService<DefaultShardingDbContext>();
var migrator = defaultShardingDbContext.GetService<IMigrator>();
migrator.Migrate("InitialCreate");
}
app.ApplicationServices.UseAutoShardingCreate();
app.ApplicationServices.UseAutoTryCompensateTable();
app.UseRouting();

View File

@ -108,7 +108,7 @@ namespace ShardingCore.Helpers
new ShardingDbContextOptions(dbContextOptions,
routeTailFactory.Create(string.Empty, false))))
{
if (( dbContext.Database.GetPendingMigrations()).Any())
if (targetMigration!=null||( dbContext.Database.GetPendingMigrations()).Any())
{
var migrator = dbContext.GetService<IMigrator>();
migrator.Migrate(targetMigration);