Skip to content

Commit

Permalink
[fix]带有分表策略的实体类不参与反向工程
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 20, 2024
1 parent 6c63b16 commit cfe2894
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 54 deletions.
91 changes: 44 additions & 47 deletions XCode/DataAccessLayer/MetaData/IMetaData.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
using System;
using System.Collections.Generic;
using NewLife;
using NewLife;

namespace XCode.DataAccessLayer
namespace XCode.DataAccessLayer;

/// <summary>数据库元数据接口</summary>
public interface IMetaData : IDisposable2
{
/// <summary>数据库元数据接口</summary>
public interface IMetaData : IDisposable2
{
#region 属性
/// <summary>数据库</summary>
IDatabase Database { get; }

/// <summary>所有元数据集合</summary>
ICollection<String> MetaDataCollections { get; }

/// <summary>保留关键字</summary>
ICollection<String> ReservedWords { get; }
#endregion

#region 构架
/// <summary>取得表模型,正向工程</summary>
/// <returns></returns>
IList<IDataTable> GetTables();

/// <summary>
/// 取得所有表名
/// </summary>
/// <returns></returns>
IList<String> GetTableNames();

/// <summary>设置表模型,检查数据表是否匹配表模型,反向工程</summary>
/// <param name="setting">设置</param>
/// <param name="tables"></param>
void SetTables(Migration setting, params IDataTable[] tables);

/// <summary>获取数据定义语句</summary>
/// <param name="schema">数据定义模式</param>
/// <param name="values">其它信息</param>
/// <returns>数据定义语句</returns>
String? GetSchemaSQL(DDLSchema schema, params Object?[] values);

/// <summary>设置数据定义模式</summary>
/// <param name="schema">数据定义模式</param>
/// <param name="values">其它信息</param>
/// <returns></returns>
Object? SetSchema(DDLSchema schema, params Object?[] values);
#endregion
}
#region 属性
/// <summary>数据库</summary>
IDatabase Database { get; }

/// <summary>所有元数据集合</summary>
ICollection<String> MetaDataCollections { get; }

/// <summary>保留关键字</summary>
ICollection<String> ReservedWords { get; }
#endregion

#region 构架
/// <summary>取得表模型,正向工程</summary>
/// <returns></returns>
IList<IDataTable> GetTables();

/// <summary>
/// 取得所有表名
/// </summary>
/// <returns></returns>
IList<String> GetTableNames();

/// <summary>设置表模型,检查数据表是否匹配表模型,反向工程</summary>
/// <param name="setting">设置</param>
/// <param name="tables"></param>
void SetTables(Migration setting, params IDataTable[] tables);

/// <summary>获取数据定义语句</summary>
/// <param name="schema">数据定义模式</param>
/// <param name="values">其它信息</param>
/// <returns>数据定义语句</returns>
String? GetSchemaSQL(DDLSchema schema, params Object?[] values);

/// <summary>设置数据定义模式</summary>
/// <param name="schema">数据定义模式</param>
/// <param name="values">其它信息</param>
/// <returns></returns>
Object? SetSchema(DDLSchema schema, params Object?[] values);
#endregion
}
14 changes: 7 additions & 7 deletions XCode/Entity/EntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,23 @@ private static void Init(String connName, IList<Type>? types, Boolean throwOnErr
// 反向工程检查
if (dal.Db.Migration > Migration.Off)
{
//var tables = facts.Select(e => e.Table.DataTable).ToArray();
var tables = new List<IDataTable>();
foreach (var item in facts)
{
// 带有分表策略的实体类不参与反向工程
if (item.ShardPolicy != null) continue;
if (item.Table.DataTable.Columns.Any(e => e.DataScale.StartsWithIgnoreCase("shard:", "timeShard:"))) continue;

// 克隆一份,防止修改
var table = item.Table.DataTable;
table = (table.Clone() as IDataTable)!;

if (/*table != null &&*/ table.TableName != item.TableName)
if (table.TableName != item.TableName)
{
// 表名去掉前缀
var name = item.TableName;
if (name.Contains(".")) name = name.Substring(".");
var p = name.IndexOf('.');
if (p > 0) name = name.Substring(p + 1);

table.TableName = name;
}
Expand All @@ -276,10 +280,6 @@ private static void Init(String connName, IList<Type>? types, Boolean throwOnErr
// 实体类初始化数据
foreach (var item in facts)
{
//if (item.Default is EntityBase entity)
//{
// entity.InitData();
//}
item.Session.InitData();

if (span != null) span.Value++;
Expand Down

0 comments on commit cfe2894

Please sign in to comment.