diff --git a/Examples/aspnetcore_transaction/Properties/launchSettings.json b/Examples/aspnetcore_transaction/Properties/launchSettings.json new file mode 100644 index 000000000..a02e18d35 --- /dev/null +++ b/Examples/aspnetcore_transaction/Properties/launchSettings.json @@ -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" + } + } +} \ No newline at end of file diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index 4697de39d..1cc4c9fe1 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -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 @@ public class BBB { 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 @@ static void Main(string[] args) 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 @@ static void Main(string[] args) 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(t => + { + t.Name("AAA_fluentapi"); + t.Property(a => a.aa).Name("AA_fluentapi"); + }); + fsql.Select(); + + fsql.Select(); + + + var sqlskdfj = fsql.Select().AsType(typeof(BBB)).ToSql(a => new CCC()); diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 26522f106..594fbad32 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -733,15 +733,6 @@ - - - 根据Assembly扫描所有继承IEntityTypeConfiguration<T>的配置类 - - - - - - 创建普通数据上下文档对象 @@ -800,14 +791,5 @@ - - - 批量注入 Repository,可以参考代码自行调整 - - - - - - diff --git a/FreeSql/FreeSqlBuilder.cs b/FreeSql/FreeSqlBuilder.cs index 3dd3dbbdf..51e3b3dc8 100644 --- a/FreeSql/FreeSqlBuilder.cs +++ b/FreeSql/FreeSqlBuilder.cs @@ -356,36 +356,36 @@ public IFreeSql Build() //添加实体属性名全局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 @@ public IFreeSql Build() 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}"; } }); diff --git a/FreeSql/FreeSqlBuilder_Obsolete.cs b/FreeSql/FreeSqlBuilder_Obsolete.cs index 41563807c..b40216284 100644 --- a/FreeSql/FreeSqlBuilder_Obsolete.cs +++ b/FreeSql/FreeSqlBuilder_Obsolete.cs @@ -52,24 +52,24 @@ void EntityPropertyNameConvert(IFreeSql fsql) //添加实体属性名全局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; diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index d183b3ea7..0f42d02e5 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -113,6 +113,7 @@ public TableAttribute GetConfigEntity(Type type) } public MappingPriorityType[] _mappingPriorityTypes = new[] { MappingPriorityType.Aop, MappingPriorityType.FluentApi, MappingPriorityType.Attribute }; + ConcurrentDictionary> dicAopConfigEntityIndex = new ConcurrentDictionary>(); public TableAttribute GetEntityTableAttribute(Type type) { var attr = new TableAttribute(); @@ -133,12 +134,21 @@ public TableAttribute GetEntityTableAttribute(Type type) 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(); + 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 @@ public IndexAttribute[] GetEntityIndexAttribute(Type type) 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);