mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 优化 UseMappingPriority 与实体元数据逻辑;#1247
This commit is contained in:
parent
903a309c92
commit
0e6945cf76
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.Extensions;
|
||||
using FreeSql.Internal;
|
||||
using FreeSql.Internal.CommonProvider;
|
||||
using FreeSql.Internal.Model;
|
||||
using Newtonsoft.Json;
|
||||
@ -341,8 +342,12 @@ namespace base_entity
|
||||
{
|
||||
public int bb { get; set; }
|
||||
}
|
||||
[Table(Name = "AAA_attr")]
|
||||
[Index("xxx1", nameof(aa))]
|
||||
[Index("xxx2", nameof(aa))]
|
||||
public class AAA
|
||||
{
|
||||
[Column(Name = "aa_attr")]
|
||||
public int aa { get; set; }
|
||||
}
|
||||
|
||||
@ -359,6 +364,8 @@ namespace base_entity
|
||||
var fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseNoneCommandParameter(true)
|
||||
.UseNameConvert(NameConvertType.ToLower)
|
||||
.UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop)
|
||||
|
||||
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=:memory:")
|
||||
@ -402,6 +409,26 @@ namespace base_entity
|
||||
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
|
||||
#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());
|
||||
|
||||
|
||||
|
@ -733,15 +733,6 @@
|
||||
<param name="modelBuilder"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlDbContextExtensions.ApplyConfigurationsFromAssembly(FreeSql.ICodeFirst,System.Reflection.Assembly,System.Func{System.Type,System.Boolean})">
|
||||
<summary>
|
||||
根据Assembly扫描所有继承IEntityTypeConfiguration<T>的配置类
|
||||
</summary>
|
||||
<param name="codeFirst"></param>
|
||||
<param name="assembly"></param>
|
||||
<param name="predicate"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlDbContextExtensions.CreateDbContext(IFreeSql)">
|
||||
<summary>
|
||||
创建普通数据上下文档对象
|
||||
@ -800,14 +791,5 @@
|
||||
<param name="that"></param>
|
||||
<returns></returns>
|
||||
</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>
|
||||
</doc>
|
||||
|
@ -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();
|
||||
@ -139,6 +140,15 @@ namespace FreeSql.Internal
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user