diff --git a/Examples/aspnetcore_transaction/Controllers/HomeController.cs b/Examples/aspnetcore_transaction/Controllers/HomeController.cs index 84d6a317..35129473 100644 --- a/Examples/aspnetcore_transaction/Controllers/HomeController.cs +++ b/Examples/aspnetcore_transaction/Controllers/HomeController.cs @@ -2,6 +2,7 @@ using FreeSql.DataAnnotations; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using System.ComponentModel; using System.Threading.Tasks; namespace aspnetcore_transaction.Controllers @@ -45,6 +46,7 @@ namespace aspnetcore_transaction.Controllers public SongService(BaseRepository repoSong, BaseRepository repoDetail, SongRepository repoSong2) { + var tb = repoSong.Orm.CodeFirst.GetTableByEntity(typeof(Song)); _repoSong = repoSong; _repoDetail = repoDetail; _repoSong2 = repoSong2; @@ -81,9 +83,14 @@ namespace aspnetcore_transaction.Controllers public SongRepository(UnitOfWorkManager uowm) : base(uowm?.Orm, uowm) { } } + [Description("123")] public class Song { + /// + /// 自增 + /// [Column(IsIdentity = true)] + [Description("自增id")] public int Id { get; set; } public string Title { get; set; } } diff --git a/Examples/aspnetcore_transaction/aspnetcore_transaction.csproj b/Examples/aspnetcore_transaction/aspnetcore_transaction.csproj index 16c8e28f..cecd9f4b 100644 --- a/Examples/aspnetcore_transaction/aspnetcore_transaction.csproj +++ b/Examples/aspnetcore_transaction/aspnetcore_transaction.csproj @@ -4,6 +4,10 @@ netcoreapp3.1 + + aspnetcore_transaction.xml + + diff --git a/Examples/aspnetcore_transaction/aspnetcore_transaction.xml b/Examples/aspnetcore_transaction/aspnetcore_transaction.xml new file mode 100644 index 00000000..8d78a341 --- /dev/null +++ b/Examples/aspnetcore_transaction/aspnetcore_transaction.xml @@ -0,0 +1,18 @@ + + + + aspnetcore_transaction + + + + + 自增 + + + + + 使用事务执行,请查看 Program.cs 代码开启动态代理 + + + + diff --git a/FreeSql.Tests/FreeSql.Tests.Provider.Odbc/PostgreSQL/Curd/PostgreSQLSelectTest.cs b/FreeSql.Tests/FreeSql.Tests.Provider.Odbc/PostgreSQL/Curd/PostgreSQLSelectTest.cs index c46c8307..f0e048a1 100644 --- a/FreeSql.Tests/FreeSql.Tests.Provider.Odbc/PostgreSQL/Curd/PostgreSQLSelectTest.cs +++ b/FreeSql.Tests/FreeSql.Tests.Provider.Odbc/PostgreSQL/Curd/PostgreSQLSelectTest.cs @@ -128,7 +128,7 @@ namespace FreeSql.Tests.Odbc.PostgreSQL var dt1 = select.Limit(10).ToDataTable(); var dt2 = select.Limit(10).ToDataTable("id, 222"); - var dt3 = select.Limit(10).ToDataTable(a => new { a.Id, a.Type.Name, now = DateTime.Now }); + var dt3 = select.Limit(10).ToDataTable(a => new { id = a.Id, name2 = a.Type.Name, now = DateTime.Now }); } class TestDto { diff --git a/FreeSql.Tests/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs index c46c8779..db9854e2 100644 --- a/FreeSql.Tests/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs @@ -128,7 +128,7 @@ namespace FreeSql.Tests.PostgreSQL var dt1 = select.Limit(10).ToDataTable(); var dt2 = select.Limit(10).ToDataTable("id, 222"); - var dt3 = select.Limit(10).ToDataTable(a => new { a.Id, a.Type.Name, now = DateTime.Now }); + var dt3 = select.Limit(10).ToDataTable(a => new { id = a.Id, name2 = a.Type.Name, now = DateTime.Now }); } class TestDto { diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index d6b6f1a8..74a353d9 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2901,6 +2901,13 @@ AsType, Ctor, ClearData 三处地方需要重新加载 + + + 动态读取 DescriptionAttribute 注释文本 + + + + 通过属性的注释文本,通过 xml 读取 diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index 9e8a337c..ef10b55a 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -371,6 +371,48 @@ namespace FreeSql.Internal return iidx == 1 ? sb.Remove(0, 5).Remove(sb.Length - 1, 1).ToString() : sb.Remove(0, 4).ToString(); } + /// + /// 动态读取 DescriptionAttribute 注释文本 + /// + /// + /// + public static Dictionary GetPropertyCommentByDescriptionAttribute(Type type) + { + var dic = new Dictionary(); + GetDydesc(null); //class注释 + + var props = type.GetPropertiesDictIgnoreCase().Values; + foreach (var prop in props) + GetDydesc(prop); + + return dic; + + void GetDydesc(PropertyInfo prop) + { + object[] attrs = null; + try + { + attrs = prop == null ? + type.GetCustomAttributes(false).ToArray() : + prop.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常 + } + catch { } + + var dyattr = attrs?.Where(a => { + return ((a as Attribute)?.TypeId as Type)?.Name == "DescriptionAttribute"; + }).FirstOrDefault(); + if (dyattr != null) + { + var valueProp = dyattr.GetType().GetProperties().Where(a => a.PropertyType == typeof(string)).FirstOrDefault(); + var comment = valueProp?.GetValue(dyattr, null)?.ToString(); + if (string.IsNullOrEmpty(comment) == false) + dic.Add(prop == null ? + "" : + prop.Name, comment); + } + } + } + /// /// 通过属性的注释文本,通过 xml 读取 /// @@ -378,6 +420,8 @@ namespace FreeSql.Internal /// Dict:key=属性名,value=注释 public static Dictionary GetProperyCommentBySummary(Type type) { + if (type.Assembly.IsDynamic) return null; + //动态生成的程序集,访问不了 Assembly.Location/Assembly.CodeBase var regex = new Regex(@"\.(dll|exe)", RegexOptions.IgnoreCase); var xmlPath = regex.Replace(type.Assembly.Location, ".xml"); if (File.Exists(xmlPath) == false) diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index debd9d0f..3d7401d3 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -69,7 +69,10 @@ namespace FreeSql.Internal var propsLazy = new List>(); var propsNavObjs = new List(); var propsComment = CommonUtils.GetProperyCommentBySummary(entity); + var propsCommentByDescAttr = CommonUtils.GetPropertyCommentByDescriptionAttribute(entity); trytb.Comment = propsComment != null && propsComment.TryGetValue("", out var tbcomment) ? tbcomment : ""; + if (string.IsNullOrEmpty(trytb.Comment) && propsCommentByDescAttr != null && propsCommentByDescAttr.TryGetValue("", out tbcomment)) trytb.Comment = tbcomment; + var columnsList = new List(); foreach (var p in trytb.Properties.Values) { @@ -148,6 +151,9 @@ namespace FreeSql.Internal }; if (propsComment != null && propsComment.TryGetValue(p.Name, out var trycomment)) col.Comment = trycomment; + if (string.IsNullOrEmpty(col.Comment) && propsCommentByDescAttr != null && propsCommentByDescAttr.TryGetValue(p.Name, out trycomment)) + col.Comment = trycomment; + if (colattr.IsIgnore) { trytb.ColumnsByCsIgnore.Add(p.Name, col);