Skip to content

Commit d78e685

Browse files
committed
- ZeroDbContext.LoadSchemasAndNavigates;
1 parent 28d3a53 commit d78e685

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Extensions/FreeSql.Extensions.ZeroEntity/ZeroDbContext.cs

+35-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,41 @@ static List<ZeroTableInfo> ValidateSchemaToInfoInternal(IFreeSql orm, IEnumerabl
273273
}
274274

275275
/// <summary>
276-
/// 从数据库中加载<para></para>
276+
/// 从自定义中加载(多表)<para></para>
277+
/// - tableName 以及 Navigates 所依赖表 Schema
278+
/// </summary>
279+
public List<TableDescriptor> LoadSchemasAndNavigates(string tableName, Func<string, TableDescriptor> getSchemaHandler)
280+
{
281+
if (getSchemaHandler == null) throw new Exception($"{nameof(getSchemaHandler)} 不能为 null");
282+
var schema = getSchemaHandler(tableName);
283+
if (schema == null) throw new Exception($"{nameof(getSchemaHandler)}({tableName}) 返回值不能为 null");
284+
var returnSchemas = new List<TableDescriptor>();
285+
returnSchemas.Add(schema);
286+
LocalEachNavigate(schema);
287+
return returnSchemas;
288+
289+
void LocalEachNavigate(TableDescriptor desc)
290+
{
291+
if (desc.Navigates?.Any() != true) return;
292+
foreach(var nav in desc.Navigates)
293+
{
294+
if (returnSchemas.Any(a => a.Name == nav.RelTable)) continue;
295+
var navSchema = getSchemaHandler(nav.RelTable);
296+
if (navSchema == null) throw new Exception($"{nameof(getSchemaHandler)}({nav.RelTable}) 返回值不能为 null");
297+
returnSchemas.Add(navSchema);
298+
LocalEachNavigate(navSchema);
299+
if (nav.Type == TableDescriptor.NavigateType.ManyToMany && !string.IsNullOrWhiteSpace(nav.ManyToMany))
300+
{
301+
var midSchema = getSchemaHandler(nav.ManyToMany);
302+
if (midSchema == null) throw new Exception($"{nameof(getSchemaHandler)}({nav.ManyToMany}) 返回值不能为 null");
303+
returnSchemas.Add(midSchema);
304+
}
305+
}
306+
}
307+
}
308+
309+
/// <summary>
310+
/// 从数据库中加载(单表)<para></para>
277311
/// - 不支持 Navigates<para></para>
278312
/// - 不支持 Indexes IndexMethod<para></para>
279313
/// - 暂支持 SqlServer/MySql decimal(10,2)(其他数据库需实现对应 IDbFirst)

0 commit comments

Comments
 (0)