mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 优化 UseMappingPriority 与实体元数据逻辑;#1247
This commit is contained in:
@ -356,36 +356,36 @@ namespace FreeSql
|
||||
//添加实体属性名全局AOP转换处理
|
||||
if (_nameConvertType != NameConvertType.None)
|
||||
{
|
||||
string PascalCaseToUnderScore(string str) => string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
|
||||
//string UnderScorePascalCase(string str) => string.Join("", str.Split('_').Select(a => a.Length > 0 ? string.Concat(char.ToUpper(a[0]), a.Substring(1)) : ""));
|
||||
string PascalCaseToUnderScore(string str) => string.IsNullOrWhiteSpace(str) ? str : string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
|
||||
//string UnderScorePascalCase(string str) => string.IsNullOrWhiteSpace(str) ? str : string.Join("", str.Split('_').Select(a => a.Length > 0 ? string.Concat(char.ToUpper(a[0]), a.Substring(1)) : ""));
|
||||
|
||||
switch (_nameConvertType)
|
||||
{
|
||||
case NameConvertType.ToLower:
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.EntityType.Name.ToLower();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToLower();
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
|
||||
ret.CodeFirst.IsSyncStructureToLower = true;
|
||||
break;
|
||||
case NameConvertType.ToUpper:
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.EntityType.Name.ToUpper();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToUpper();
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
|
||||
ret.CodeFirst.IsSyncStructureToUpper = true;
|
||||
break;
|
||||
case NameConvertType.PascalCaseToUnderscore:
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name);
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name);
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
|
||||
break;
|
||||
case NameConvertType.PascalCaseToUnderscoreWithLower:
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name).ToLower();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToLower();
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
|
||||
break;
|
||||
case NameConvertType.PascalCaseToUnderscoreWithUpper:
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name).ToUpper();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToUpper();
|
||||
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
|
||||
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
|
||||
break;
|
||||
//case NameConvertType.UnderscoreToPascalCase:
|
||||
// ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.EntityType.Name);
|
||||
// ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.Property.Name);
|
||||
// ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.ModifyResult.Name);
|
||||
// ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.ModifyResult.Name);
|
||||
// break;
|
||||
default:
|
||||
break;
|
||||
@ -508,7 +508,7 @@ namespace FreeSql
|
||||
else if (string.IsNullOrEmpty(name) == false)
|
||||
e.ModifyResult.Name = name;
|
||||
else if (string.IsNullOrEmpty(schema) == false)
|
||||
e.ModifyResult.Name = $"{schema}.{e.EntityType.Name}";
|
||||
e.ModifyResult.Name = $"{schema}.{e.ModifyResult.Name}";
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -52,24 +52,24 @@ namespace FreeSql
|
||||
//添加实体属性名全局AOP转换处理
|
||||
if (_entityPropertyConvertType != StringConvertType.None)
|
||||
{
|
||||
string PascalCaseToUnderScore(string str) => string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
|
||||
string PascalCaseToUnderScore(string str) => string.IsNullOrWhiteSpace(str) ? str : string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
|
||||
|
||||
switch (_entityPropertyConvertType)
|
||||
{
|
||||
case StringConvertType.Lower:
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToLower();
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
|
||||
break;
|
||||
case StringConvertType.Upper:
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToUpper();
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
|
||||
break;
|
||||
case StringConvertType.PascalCaseToUnderscore:
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name);
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
|
||||
break;
|
||||
case StringConvertType.PascalCaseToUnderscoreWithLower:
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToLower();
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
|
||||
break;
|
||||
case StringConvertType.PascalCaseToUnderscoreWithUpper:
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToUpper();
|
||||
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -113,6 +113,7 @@ namespace FreeSql.Internal
|
||||
}
|
||||
|
||||
public MappingPriorityType[] _mappingPriorityTypes = new[] { MappingPriorityType.Aop, MappingPriorityType.FluentApi, MappingPriorityType.Attribute };
|
||||
ConcurrentDictionary<Type, Dictionary<string, IndexAttribute>> dicAopConfigEntityIndex = new ConcurrentDictionary<Type, Dictionary<string, IndexAttribute>>();
|
||||
public TableAttribute GetEntityTableAttribute(Type type)
|
||||
{
|
||||
var attr = new TableAttribute();
|
||||
@ -133,12 +134,21 @@ namespace FreeSql.Internal
|
||||
AsTable = attr.AsTable
|
||||
}
|
||||
};
|
||||
_orm.Aop.ConfigEntityHandler(_orm, aope);
|
||||
_orm.Aop.ConfigEntityHandler(_orm, aope);
|
||||
var tryattr = aope.ModifyResult;
|
||||
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
|
||||
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
|
||||
if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure;
|
||||
if (!string.IsNullOrEmpty(tryattr.AsTable)) attr.AsTable = tryattr.AsTable;
|
||||
|
||||
var indexs = new Dictionary<string, IndexAttribute>();
|
||||
foreach (var idxattr in aope.ModifyIndexResult)
|
||||
if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
|
||||
{
|
||||
if (indexs.ContainsKey(idxattr.Name)) indexs.Remove(idxattr.Name);
|
||||
indexs.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
|
||||
}
|
||||
dicAopConfigEntityIndex.AddOrUpdate(type, indexs, (_, old) => indexs);
|
||||
}
|
||||
break;
|
||||
case MappingPriorityType.FluentApi:
|
||||
@ -357,11 +367,9 @@ namespace FreeSql.Internal
|
||||
switch (mp)
|
||||
{
|
||||
case MappingPriorityType.Aop:
|
||||
if (_orm.Aop.ConfigEntityHandler != null)
|
||||
if (dicAopConfigEntityIndex.TryGetValue(type, out var tryidxs))
|
||||
{
|
||||
var aope = new Aop.ConfigEntityEventArgs(type);
|
||||
_orm.Aop.ConfigEntityHandler(_orm, aope);
|
||||
foreach (var idxattr in aope.ModifyIndexResult)
|
||||
foreach (var idxattr in tryidxs.Values)
|
||||
if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
|
||||
{
|
||||
if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name);
|
||||
|
Reference in New Issue
Block a user