diff --git a/samples/Sample.AutoCreateIfPresent/DefaultDbContext.cs b/samples/Sample.AutoCreateIfPresent/DefaultDbContext.cs index 396cad2a..d1182b02 100644 --- a/samples/Sample.AutoCreateIfPresent/DefaultDbContext.cs +++ b/samples/Sample.AutoCreateIfPresent/DefaultDbContext.cs @@ -14,6 +14,7 @@ namespace Sample.AutoCreateIfPresent { public class DefaultDbContext:AbstractShardingDbContext,IShardingTableDbContext { + public DefaultDbContext(DbContextOptions options) : base(options) { } diff --git a/samples/Sample.AutoCreateIfPresent/Program.cs b/samples/Sample.AutoCreateIfPresent/Program.cs index e57e184f..aef2b64e 100644 --- a/samples/Sample.AutoCreateIfPresent/Program.cs +++ b/samples/Sample.AutoCreateIfPresent/Program.cs @@ -2,6 +2,8 @@ using Microsoft.EntityFrameworkCore; using Sample.AutoCreateIfPresent; using ShardingCore; using ShardingCore.Bootstrappers; +using ShardingCore.Core.DbContextCreator; +using ShardingCore.Core.RuntimeContexts; using ShardingCore.TableExists; using ShardingCore.TableExists.Abstractions; @@ -38,7 +40,8 @@ builder.Services.AddShardingDbContext() { b.UseMySql(conn, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger); }); - }).ReplaceService().AddShardingCore(); + }) + .ReplaceService().AddShardingCore(); var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs b/samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs index cedca253..2609c82e 100644 --- a/samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs +++ b/samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs @@ -14,7 +14,7 @@ namespace Sample.MySql.Domain.Maps { builder.HasKey(o => o.Id); builder.Property(o => o.Id).IsRequired().HasMaxLength(128); - builder.ToTable(nameof(SysUserLogByMonth)); + builder.ToTable("Sys_User_LogBy_Month"); } } } \ No newline at end of file diff --git a/samples/Sample.MySql/Migrations/20220704042701_init.cs b/samples/Sample.MySql/Migrations/20220704042701_init.cs index 49ad62bb..ec0b641a 100644 --- a/samples/Sample.MySql/Migrations/20220704042701_init.cs +++ b/samples/Sample.MySql/Migrations/20220704042701_init.cs @@ -28,7 +28,7 @@ namespace Sample.MySql.Migrations .Annotation("MySql:CharSet", "utf8mb4"); migrationBuilder.CreateTable( - name: "SysUserLogByMonth", + name: "Sys_User_LogBy_Month", columns: table => new { Id = table.Column(type: "varchar(128)", maxLength: 128, nullable: false) @@ -37,7 +37,7 @@ namespace Sample.MySql.Migrations }, constraints: table => { - table.PrimaryKey("PK_SysUserLogByMonth", x => x.Id); + table.PrimaryKey("PK_Sys_User_LogBy_Month", x => x.Id); }) .Annotation("MySql:CharSet", "utf8mb4"); @@ -64,7 +64,7 @@ namespace Sample.MySql.Migrations name: "SysTest"); migrationBuilder.DropTable( - name: "SysUserLogByMonth"); + name: "Sys_User_LogBy_Month"); migrationBuilder.DropTable( name: "SysUserMod"); diff --git a/src/ShardingCore/Helpers/MigrationHelper.cs b/src/ShardingCore/Helpers/MigrationHelper.cs index e604e162..c7fc3fa0 100644 --- a/src/ShardingCore/Helpers/MigrationHelper.cs +++ b/src/ShardingCore/Helpers/MigrationHelper.cs @@ -158,12 +158,15 @@ namespace ShardingCore.Helpers shardings.ForEach(aShardingTable => { string newCmd = sourceCmd; - GetReplaceGroups(operation, absTableName, aShardingTable).ForEach(aReplace => + var replaceGroups = GetReplaceGroups(operation, absTableName, aShardingTable); + foreach (var migrationReplaceItem in replaceGroups) { + var delimitSourceNameIdentifier = sqlGenerationHelper.DelimitIdentifier(migrationReplaceItem.SourceName); + var delimitTargetNameIdentifier = sqlGenerationHelper.DelimitIdentifier(migrationReplaceItem.TargetName); newCmd = newCmd.Replace( - sqlGenerationHelper.DelimitIdentifier(aReplace.sourceName), - sqlGenerationHelper.DelimitIdentifier(aReplace.targetName)); - }); + delimitSourceNameIdentifier, + delimitTargetNameIdentifier); + } if (newCmd.Contains( "EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE'")) { @@ -185,13 +188,13 @@ namespace ShardingCore.Helpers } } - private static List<(string sourceName, string targetName)> GetReplaceGroups( + private static ISet GetReplaceGroups( MigrationOperation operation, string sourceTableName, string targetTableName) { - List<(string sourceName, string targetName)> resList = - new List<(string sourceName, string targetName)> + ISet resList = + new HashSet() { - (sourceTableName, targetTableName) + new MigrationReplaceItem(sourceTableName, targetTableName) }; string name = operation.GetPropertyValue("Name") as string; @@ -209,7 +212,7 @@ namespace ShardingCore.Helpers if (Regex.IsMatch(name, aPattern)) { var newName = new Regex(aPattern).Replace(name, "${1}" + targetTableName + "$3"); - resList.Add((name, newName)); + resList.Add(new MigrationReplaceItem(name, newName)); break; } } @@ -235,14 +238,22 @@ namespace ShardingCore.Helpers var propertyValue = aProperty.GetValue(operation); if (propertyValue is MigrationOperation propertyOperation) { - resList.AddRange(GetReplaceGroups(propertyOperation, sourceTableName, targetTableName)); + var migrationReplaceItems = GetReplaceGroups(propertyOperation, sourceTableName, targetTableName); + foreach (var migrationReplaceItem in migrationReplaceItems) + { + resList.Add(migrationReplaceItem); + } } else if (listPropertyWhere(aProperty)) { foreach (var aValue in (IEnumerable)propertyValue) { - resList.AddRange(GetReplaceGroups((MigrationOperation)aValue, sourceTableName, - targetTableName)); + var migrationReplaceItems = GetReplaceGroups((MigrationOperation)aValue, sourceTableName, + targetTableName); + foreach (var migrationReplaceItem in migrationReplaceItems) + { + resList.Add(migrationReplaceItem); + } } } }); diff --git a/src/ShardingCore/Helpers/MigrationReplaceItem.cs b/src/ShardingCore/Helpers/MigrationReplaceItem.cs new file mode 100644 index 00000000..746063d7 --- /dev/null +++ b/src/ShardingCore/Helpers/MigrationReplaceItem.cs @@ -0,0 +1,46 @@ +using System; + +namespace ShardingCore.Helpers +{ + + public class MigrationReplaceItem + { + public string SourceName { get; } + public string TargetName { get; } + + public MigrationReplaceItem(string sourceName,string targetName) + { + SourceName = sourceName; + TargetName = targetName; + } + protected bool Equals(MigrationReplaceItem other) + { + return SourceName == other.SourceName && TargetName == other.TargetName; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((MigrationReplaceItem)obj); + } +#if !EFCORE2 + + public override int GetHashCode() + { + return HashCode.Combine(SourceName, TargetName); + } +#endif +#if EFCORE2 + + public override int GetHashCode() + { + unchecked + { + return ((SourceName != null ? SourceName.GetHashCode() : 0) * 397) ^ (TargetName != null ? TargetName.GetHashCode() : 0); + } + } +#endif + } +}