mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 增加 AsTable 和 Repository 分表时的自动迁移分表功能;
- 增加 ICodeFirst.SyncStructure(Type entityType, string tableName) 指定表名来迁移实体; ```csharp fsql.CodeFirst.SyncStructure(typeof(Log), "Log_1"); //迁移到 Log_1 表 fsql.CodeFirst.SyncStructure(typeof(Log), "Log_2"); //迁移到 Log_2 表 ```
This commit is contained in:
		@@ -103,7 +103,7 @@ ELSE
 | 
			
		||||
    , @level2type = 'COLUMN', @level2name = N'{2}'
 | 
			
		||||
", schema.Replace("'", "''"), table.Replace("'", "''"), column.Replace("'", "''"), comment?.Replace("'", "''") ?? "");
 | 
			
		||||
        }
 | 
			
		||||
        public override string GetComparisonDDLStatements(params Type[] entityTypes)
 | 
			
		||||
        protected override string GetComparisonDDLStatements(params (Type entityType, string tableName)[] objects)
 | 
			
		||||
        {
 | 
			
		||||
            var conn = _orm.Ado.MasterPool.Get(TimeSpan.FromSeconds(5));
 | 
			
		||||
            var database = conn.Value.Database;
 | 
			
		||||
@@ -127,12 +127,12 @@ ELSE
 | 
			
		||||
            var sb = new StringBuilder();
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                foreach (var entityType in entityTypes)
 | 
			
		||||
                foreach (var obj in objects)
 | 
			
		||||
                {
 | 
			
		||||
                    if (sb.Length > 0) sb.Append("\r\n");
 | 
			
		||||
                    var tb = _commonUtils.GetTableByEntity(entityType);
 | 
			
		||||
                    if (tb == null) throw new Exception($"类型 {entityType.FullName} 不可迁移");
 | 
			
		||||
                    if (tb.Columns.Any() == false) throw new Exception($"类型 {entityType.FullName} 不可迁移,可迁移属性0个");
 | 
			
		||||
                    var tb = _commonUtils.GetTableByEntity(obj.entityType);
 | 
			
		||||
                    if (tb == null) throw new Exception($"类型 {obj.entityType.FullName} 不可迁移");
 | 
			
		||||
                    if (tb.Columns.Any() == false) throw new Exception($"类型 {obj.entityType.FullName} 不可迁移,可迁移属性0个");
 | 
			
		||||
                    var tbname = tb.DbName.Split(new[] { '.' }, 3);
 | 
			
		||||
                    if (tbname?.Length == 1) tbname = new[] { database, "dbo", tbname[0] };
 | 
			
		||||
                    if (tbname?.Length == 2) tbname = new[] { database, tbname[0], tbname[1] };
 | 
			
		||||
@@ -140,6 +140,17 @@ ELSE
 | 
			
		||||
                    var tboldname = tb.DbOldName?.Split(new[] { '.' }, 3); //旧表名
 | 
			
		||||
                    if (tboldname?.Length == 1) tboldname = new[] { database, "dbo", tboldname[0] };
 | 
			
		||||
                    if (tboldname?.Length == 2) tboldname = new[] { database, tboldname[0], tboldname[1] };
 | 
			
		||||
                    if (string.IsNullOrEmpty(obj.tableName) == false)
 | 
			
		||||
                    {
 | 
			
		||||
                        var tbtmpname = obj.tableName.Split(new[] { '.' }, 3);
 | 
			
		||||
                        if (tbtmpname?.Length == 1) tbtmpname = new[] { database, "dbo", tbtmpname[0] };
 | 
			
		||||
                        if (tbtmpname?.Length == 2) tbtmpname = new[] { database, tbtmpname[0], tbtmpname[1] };
 | 
			
		||||
                        if (tbname[0] != tbtmpname[0] || tbname[1] != tbtmpname[1] || tbname[2] != tbtmpname[2])
 | 
			
		||||
                        {
 | 
			
		||||
                            tbname = tbtmpname;
 | 
			
		||||
                            tboldname = null;
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if (string.Compare(tbname[0], database, true) != 0 && ExecuteScalar(database, $" select 1 from sys.databases where name='{tbname[0]}'") == null) //创建数据库
 | 
			
		||||
                        ExecuteScalar(database, $"if not exists(select 1 from sys.databases where name='{tbname[0]}')\r\n\tcreate database [{tbname[0]}];");
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user