- 增加 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:
28810
2019-11-13 19:57:44 +08:00
parent 24e2c098a4
commit dda7c8bc9c
21 changed files with 254 additions and 101 deletions

View File

@ -2355,7 +2355,15 @@
<summary>
将实体类型集合与数据库对比返回DDL语句
</summary>
<param name="entityTypes"></param>
<param name="entityTypes">实体类型</param>
<returns></returns>
</member>
<member name="M:FreeSql.ICodeFirst.GetComparisonDDLStatements(System.Type,System.String)">
<summary>
将实体类型与数据库对比返回DDL语句指定表名
</summary>
<param name="entityType">实体类型</param>
<param name="tableName">指定表名对比</param>
<returns></returns>
</member>
<member name="M:FreeSql.ICodeFirst.SyncStructure``1">
@ -2372,6 +2380,14 @@
<param name="entityTypes"></param>
<returns></returns>
</member>
<member name="M:FreeSql.ICodeFirst.SyncStructure(System.Type,System.String)">
<summary>
同步实体类型到数据库(指定表名)
</summary>
<param name="entityType">实体类型</param>
<param name="tableName">指定表名对比</param>
<returns></returns>
</member>
<member name="M:FreeSql.ICodeFirst.GetDbInfo(System.Type)">
<summary>
根据 System.Type 获取数据库信息

View File

@ -44,10 +44,17 @@ namespace FreeSql
/// <summary>
/// 将实体类型集合与数据库对比返回DDL语句
/// </summary>
/// <param name="entityTypes"></param>
/// <param name="entityTypes">实体类型</param>
/// <returns></returns>
string GetComparisonDDLStatements(params Type[] entityTypes);
/// <summary>
/// 将实体类型与数据库对比返回DDL语句指定表名
/// </summary>
/// <param name="entityType">实体类型</param>
/// <param name="tableName">指定表名对比</param>
/// <returns></returns>
string GetComparisonDDLStatements(Type entityType, string tableName);
/// <summary>
/// 同步实体类型到数据库
/// </summary>
/// <typeparam name="TEntity"></typeparam>
@ -59,6 +66,13 @@ namespace FreeSql
/// <param name="entityTypes"></param>
/// <returns></returns>
bool SyncStructure(params Type[] entityTypes);
/// <summary>
/// 同步实体类型到数据库(指定表名)
/// </summary>
/// <param name="entityType">实体类型</param>
/// <param name="tableName">指定表名对比</param>
/// <returns></returns>
bool SyncStructure(Type entityType, string tableName);
/// <summary>
/// 根据 System.Type 获取数据库信息

View File

@ -41,18 +41,48 @@ namespace FreeSql.Internal.CommonProvider
public TableAttribute GetConfigEntity(Type type) => _commonUtils.GetConfigEntity(type);
public TableInfo GetTableByEntity(Type type) => _commonUtils.GetTableByEntity(type);
public string GetComparisonDDLStatements<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
public abstract string GetComparisonDDLStatements(params Type[] entityTypes);
protected string GetTableNameLowerOrUpper(string tableName)
{
if (string.IsNullOrEmpty(tableName)) return "";
if (IsSyncStructureToLower) tableName = tableName.ToLower();
if (IsSyncStructureToUpper) tableName = tableName.ToUpper();
return tableName;
}
public string GetComparisonDDLStatements<TEntity>() =>
this.GetComparisonDDLStatements((typeof(TEntity), ""));
public string GetComparisonDDLStatements(params Type[] entityTypes) => entityTypes == null ? null :
this.GetComparisonDDLStatements(entityTypes.Distinct().Select(a => (a, "")).ToArray());
public string GetComparisonDDLStatements(Type entityType, string tableName) =>
this.GetComparisonDDLStatements((entityType, GetTableNameLowerOrUpper(tableName)));
protected abstract string GetComparisonDDLStatements(params (Type entityType, string tableName)[] objects);
static object syncStructureLock = new object();
internal ConcurrentDictionary<Type, bool> dicSyced = new ConcurrentDictionary<Type, bool>();
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
public bool SyncStructure(params Type[] entityTypes)
object _dicSycedLock = new object();
Dictionary<Type, ConcurrentDictionary<string, bool>> _dicSynced = new Dictionary<Type, ConcurrentDictionary<string, bool>>();
internal ConcurrentDictionary<string, bool> _dicSycedGetOrAdd(Type entityType)
{
if (entityTypes == null) return false;
var syncTypes = entityTypes.Where(a => dicSyced.ContainsKey(a) == false && GetTableByEntity(a)?.DisableSyncStructure == false).ToArray().Distinct().ToArray();
if (syncTypes.Any() == false) return false;
var before = new Aop.SyncStructureBeforeEventArgs(entityTypes);
if (_dicSynced.TryGetValue(entityType, out var trydic) == false)
lock (_dicSycedLock)
if (_dicSynced.TryGetValue(entityType, out trydic) == false)
_dicSynced.Add(entityType, trydic = new ConcurrentDictionary<string, bool>());
return trydic;
}
internal void _dicSycedTryAdd(Type entityType, string tableName = null) =>
_dicSycedGetOrAdd(entityType).TryAdd(GetTableNameLowerOrUpper(tableName), true);
public bool SyncStructure<TEntity>() =>
this.SyncStructure((typeof(TEntity), ""));
public bool SyncStructure(params Type[] entityTypes) => entityTypes == null ? false :
this.SyncStructure(entityTypes.Distinct().Select(a => (a, "")).ToArray());
public bool SyncStructure(Type entityType, string tableName) =>
this.SyncStructure((entityType, GetTableNameLowerOrUpper(tableName)));
protected bool SyncStructure(params (Type entityType, string tableName)[] objects)
{
if (objects == null) return false;
(Type entityType, string tableName)[] syncObjects = objects.Where(a => _dicSycedGetOrAdd(a.entityType).ContainsKey(GetTableNameLowerOrUpper(a.tableName)) == false && GetTableByEntity(a.entityType)?.DisableSyncStructure == false)
.Select(a => (a.entityType, GetTableNameLowerOrUpper(a.tableName))).ToArray();
if (syncObjects.Any() == false) return false;
var before = new Aop.SyncStructureBeforeEventArgs(syncObjects.Select(a => a.entityType).ToArray());
_orm.Aop.SyncStructureBefore?.Invoke(this, before);
Exception exception = null;
string ddl = null;
@ -60,14 +90,14 @@ namespace FreeSql.Internal.CommonProvider
{
lock (syncStructureLock)
{
ddl = this.GetComparisonDDLStatements(syncTypes);
ddl = this.GetComparisonDDLStatements(syncObjects);
if (string.IsNullOrEmpty(ddl))
{
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType, true);
foreach (var syncObject in syncObjects) _dicSycedTryAdd(syncObject.entityType, syncObject.tableName);
return true;
}
var affrows = ExecuteDDLStatements(ddl);
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType, true);
foreach (var syncObject in syncObjects) _dicSycedTryAdd(syncObject.entityType, syncObject.tableName);
return true;
}
}

View File

@ -123,6 +123,7 @@ namespace FreeSql.Internal.CommonProvider
if (string.IsNullOrEmpty(newname)) return _table.DbName;
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
return newname;
}
public IDelete<T1> AsTable(Func<string, string> tableRule)

View File

@ -393,6 +393,7 @@ namespace FreeSql.Internal.CommonProvider
if (string.IsNullOrEmpty(newname)) return _table.DbName;
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
return newname;
}
public IInsert<T1> AsTable(Func<string, string> tableRule)

View File

@ -895,6 +895,7 @@ namespace FreeSql.Internal.CommonProvider
{
if (_orm.CodeFirst.IsSyncStructureToLower) name = name.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) name = name.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(tb.Table.Type, name);
}
}
dict.Add(tb.Table.Type, name);

View File

@ -183,7 +183,7 @@ namespace FreeSql.Internal.CommonProvider
if (typeof(TReturn) == typeof(T1)) return this as ISelect<TReturn>;
_tables[0].Parameter = select.Parameters[0];
_selectExpression = select.Body;
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TReturn), true);
(_orm.CodeFirst as CodeFirstProvider)._dicSycedTryAdd(typeof(TReturn)); //._dicSyced.TryAdd(typeof(TReturn), true);
var ret = _orm.Select<TReturn>();
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
return ret;
@ -198,7 +198,7 @@ namespace FreeSql.Internal.CommonProvider
), SelectTableInfoType.InnerJoin);
if (typeof(TResult) == typeof(T1)) return this as ISelect<TResult>;
_selectExpression = resultSelector.Body;
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TResult), true);
(_orm.CodeFirst as CodeFirstProvider)._dicSycedTryAdd(typeof(TResult)); //._dicSyced.TryAdd(typeof(TResult), true);
var ret = _orm.Select<TResult>() as Select1Provider<TResult>;
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
return ret;
@ -213,7 +213,7 @@ namespace FreeSql.Internal.CommonProvider
), SelectTableInfoType.InnerJoin);
if (typeof(TResult) == typeof(T1)) return this as ISelect<TResult>;
_selectExpression = resultSelector.Body;
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TResult), true);
(_orm.CodeFirst as CodeFirstProvider)._dicSycedTryAdd(typeof(TResult)); //._dicSyced.TryAdd(typeof(TResult), true);
var ret = _orm.Select<TResult>() as Select1Provider<TResult>;
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
return ret;
@ -245,7 +245,7 @@ namespace FreeSql.Internal.CommonProvider
}
if (typeof(TResult) == typeof(T1)) return this as ISelect<TResult>;
_selectExpression = resultSelector.Body;
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TResult), true);
(_orm.CodeFirst as CodeFirstProvider)._dicSycedTryAdd(typeof(TResult)); //._dicSyced.TryAdd(typeof(TResult), true);
var ret = _orm.Select<TResult>() as Select1Provider<TResult>;
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
return ret;

View File

@ -515,6 +515,7 @@ namespace FreeSql.Internal.CommonProvider
if (string.IsNullOrEmpty(newname)) return _table.DbName;
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
return newname;
}
public IUpdate<T1> AsTable(Func<string, string> tableRule)