- 优化 UseMappingPriority 与实体元数据逻辑;#1247

This commit is contained in:
2881099 2022-09-19 13:12:54 +08:00
parent 903a309c92
commit 0e6945cf76
6 changed files with 88 additions and 44 deletions

View File

@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64375/",
"sslPort": 44336
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"aspnetcore_transaction": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}

View File

@ -1,6 +1,7 @@
using FreeSql; using FreeSql;
using FreeSql.DataAnnotations; using FreeSql.DataAnnotations;
using FreeSql.Extensions; using FreeSql.Extensions;
using FreeSql.Internal;
using FreeSql.Internal.CommonProvider; using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model; using FreeSql.Internal.Model;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -341,8 +342,12 @@ namespace base_entity
{ {
public int bb { get; set; } public int bb { get; set; }
} }
[Table(Name = "AAA_attr")]
[Index("xxx1", nameof(aa))]
[Index("xxx2", nameof(aa))]
public class AAA public class AAA
{ {
[Column(Name = "aa_attr")]
public int aa { get; set; } public int aa { get; set; }
} }
@ -359,6 +364,8 @@ namespace base_entity
var fsql = new FreeSql.FreeSqlBuilder() var fsql = new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true) .UseAutoSyncStructure(true)
.UseNoneCommandParameter(true) .UseNoneCommandParameter(true)
.UseNameConvert(NameConvertType.ToLower)
.UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop)
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=:memory:") .UseConnectionString(FreeSql.DataType.Sqlite, "data source=:memory:")
@ -402,6 +409,26 @@ namespace base_entity
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
fsql.Aop.ConfigEntity += (_, e) =>
{
Console.WriteLine("Aop.ConfigEntity: " + e.ModifyResult.Name);
e.ModifyIndexResult.Add(new IndexAttribute("xxx2", "aa", true));
};
fsql.Aop.ConfigEntityProperty += (_, e) =>
{
Console.WriteLine("Aop.ConfigEntityProperty: " + e.ModifyResult.Name);
};
fsql.CodeFirst.ConfigEntity<AAA>(t =>
{
t.Name("AAA_fluentapi");
t.Property(a => a.aa).Name("AA_fluentapi");
});
fsql.Select<AAA>();
fsql.Select<AAA>();
var sqlskdfj = fsql.Select<object>().AsType(typeof(BBB)).ToSql(a => new CCC()); var sqlskdfj = fsql.Select<object>().AsType(typeof(BBB)).ToSql(a => new CCC());

View File

@ -733,15 +733,6 @@
<param name="modelBuilder"></param> <param name="modelBuilder"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSqlDbContextExtensions.ApplyConfigurationsFromAssembly(FreeSql.ICodeFirst,System.Reflection.Assembly,System.Func{System.Type,System.Boolean})">
<summary>
根据Assembly扫描所有继承IEntityTypeConfiguration&lt;T&gt;的配置类
</summary>
<param name="codeFirst"></param>
<param name="assembly"></param>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSqlDbContextExtensions.CreateDbContext(IFreeSql)"> <member name="M:FreeSqlDbContextExtensions.CreateDbContext(IFreeSql)">
<summary> <summary>
创建普通数据上下文档对象 创建普通数据上下文档对象
@ -800,14 +791,5 @@
<param name="that"></param> <param name="that"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members> </members>
</doc> </doc>

View File

@ -356,36 +356,36 @@ namespace FreeSql
//添加实体属性名全局AOP转换处理 //添加实体属性名全局AOP转换处理
if (_nameConvertType != NameConvertType.None) if (_nameConvertType != NameConvertType.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()));
//string UnderScorePascalCase(string str) => string.Join("", str.Split('_').Select(a => a.Length > 0 ? string.Concat(char.ToUpper(a[0]), a.Substring(1)) : "")); //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) switch (_nameConvertType)
{ {
case NameConvertType.ToLower: case NameConvertType.ToLower:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.EntityType.Name.ToLower(); ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToLower(); ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
ret.CodeFirst.IsSyncStructureToLower = true; ret.CodeFirst.IsSyncStructureToLower = true;
break; break;
case NameConvertType.ToUpper: case NameConvertType.ToUpper:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.EntityType.Name.ToUpper(); ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToUpper(); ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
ret.CodeFirst.IsSyncStructureToUpper = true; ret.CodeFirst.IsSyncStructureToUpper = true;
break; break;
case NameConvertType.PascalCaseToUnderscore: case NameConvertType.PascalCaseToUnderscore:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name); ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name); ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
break; break;
case NameConvertType.PascalCaseToUnderscoreWithLower: case NameConvertType.PascalCaseToUnderscoreWithLower:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name).ToLower(); ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToLower(); ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
break; break;
case NameConvertType.PascalCaseToUnderscoreWithUpper: case NameConvertType.PascalCaseToUnderscoreWithUpper:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name).ToUpper(); ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToUpper(); ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
break; break;
//case NameConvertType.UnderscoreToPascalCase: //case NameConvertType.UnderscoreToPascalCase:
// ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.EntityType.Name); // ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.ModifyResult.Name);
// ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.Property.Name); // ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.ModifyResult.Name);
// break; // break;
default: default:
break; break;
@ -508,7 +508,7 @@ namespace FreeSql
else if (string.IsNullOrEmpty(name) == false) else if (string.IsNullOrEmpty(name) == false)
e.ModifyResult.Name = name; e.ModifyResult.Name = name;
else if (string.IsNullOrEmpty(schema) == false) else if (string.IsNullOrEmpty(schema) == false)
e.ModifyResult.Name = $"{schema}.{e.EntityType.Name}"; e.ModifyResult.Name = $"{schema}.{e.ModifyResult.Name}";
} }
}); });

View File

@ -52,24 +52,24 @@ namespace FreeSql
//添加实体属性名全局AOP转换处理 //添加实体属性名全局AOP转换处理
if (_entityPropertyConvertType != StringConvertType.None) 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) switch (_entityPropertyConvertType)
{ {
case StringConvertType.Lower: 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; break;
case StringConvertType.Upper: 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; break;
case StringConvertType.PascalCaseToUnderscore: 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; break;
case StringConvertType.PascalCaseToUnderscoreWithLower: 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; break;
case StringConvertType.PascalCaseToUnderscoreWithUpper: 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; break;
default: default:
break; break;

View File

@ -113,6 +113,7 @@ namespace FreeSql.Internal
} }
public MappingPriorityType[] _mappingPriorityTypes = new[] { MappingPriorityType.Aop, MappingPriorityType.FluentApi, MappingPriorityType.Attribute }; 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) public TableAttribute GetEntityTableAttribute(Type type)
{ {
var attr = new TableAttribute(); var attr = new TableAttribute();
@ -139,6 +140,15 @@ namespace FreeSql.Internal
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName; if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure; if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure;
if (!string.IsNullOrEmpty(tryattr.AsTable)) attr.AsTable = tryattr.AsTable; 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; break;
case MappingPriorityType.FluentApi: case MappingPriorityType.FluentApi:
@ -357,11 +367,9 @@ namespace FreeSql.Internal
switch (mp) switch (mp)
{ {
case MappingPriorityType.Aop: case MappingPriorityType.Aop:
if (_orm.Aop.ConfigEntityHandler != null) if (dicAopConfigEntityIndex.TryGetValue(type, out var tryidxs))
{ {
var aope = new Aop.ConfigEntityEventArgs(type); foreach (var idxattr in tryidxs.Values)
_orm.Aop.ConfigEntityHandler(_orm, aope);
foreach (var idxattr in aope.ModifyIndexResult)
if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
{ {
if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name);