From a21eac368cf713a07adfda2aa0aca16769d627d9 Mon Sep 17 00:00:00 2001 From: xiaopinzi Date: Wed, 23 Feb 2022 15:22:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ECodefirst=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=BD=93=E8=A1=A8=E5=90=8D=EF=BC=8C=E5=AD=97=E6=AE=B5=E5=90=8D?= =?UTF-8?q?=E6=9C=AA=E5=9C=A8=E7=89=B9=E6=80=A7=E4=B8=AD=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=97=B6=E5=88=99=E4=BB=8E=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E4=B8=AD=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EntityMaintenance/EntityMaintenance.cs | 70 ++++++++++++++++--- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index 75528ca3a..3f3dbaa9a 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; +using System.Xml.Linq; namespace SqlSugar { @@ -17,7 +17,7 @@ namespace SqlSugar } public EntityInfo GetEntityInfo(Type type) { - string cacheKey = "GetEntityInfo"+ type.GetHashCode() + type.FullName; + string cacheKey = "GetEntityInfo" + type.GetHashCode() + type.FullName; return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => { @@ -37,6 +37,7 @@ namespace SqlSugar result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll; result.IsDisabledDelete = sugarTable.IsDisabledDelete; } + if (result.TableDescription.IsNullOrEmpty()) result.TableDescription = GetTableAnnotation(type); if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService != null) { if (result.DbTableName == null) @@ -92,9 +93,9 @@ namespace SqlSugar } public string GetDbColumnName(string propertyName) { - return GetDbColumnName(propertyName,typeof(T)); + return GetDbColumnName(propertyName, typeof(T)); } - public string GetDbColumnName(string propertyName,Type entityType) + public string GetDbColumnName(string propertyName, Type entityType) { var isAny = this.GetEntityInfo(entityType).Columns.Any(it => it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase)); Check.Exception(!isAny, "Property " + propertyName + " is Invalid"); @@ -117,17 +118,17 @@ namespace SqlSugar if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) return dbColumnName; else { - var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.DbColumnName.Equals(dbColumnName,StringComparison.CurrentCultureIgnoreCase)); + var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.DbColumnName.Equals(dbColumnName, StringComparison.CurrentCultureIgnoreCase)); return mappingInfo == null ? dbColumnName : mappingInfo.PropertyName; } } - public string GetPropertyName(string dbColumnName,Type entityType) + public string GetPropertyName(string dbColumnName, Type entityType) { var typeName = entityType.Name; if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) return dbColumnName; else { - var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.DbColumnName.Equals(dbColumnName,StringComparison.CurrentCultureIgnoreCase)); + var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.DbColumnName.Equals(dbColumnName, StringComparison.CurrentCultureIgnoreCase)); return mappingInfo == null ? dbColumnName : mappingInfo.PropertyName; } } @@ -136,6 +137,52 @@ namespace SqlSugar var propertyName = GetPropertyName(dbColumnName); return typeof(T).GetProperties().First(it => it.Name == propertyName); } + /// + /// Gets the text contents of this XML element node + /// + /// entity type + /// The value of the name attribute of the XML node + /// the text contents of this XML element node + public string GetXElementNodeValue(Type entityType, string nodeAttributeName) + { + FileInfo file = new FileInfo(entityType.Assembly.Location); + string xmlPath = entityType.Assembly.Location.Replace(file.Extension, ".xml"); + if (!File.Exists(xmlPath)) + { + return string.Empty; + } + XElement xe = XElement.Load(xmlPath); + if (xe == null) + { + return string.Empty; + } + var xeNode = xe.Element("members").Elements("member").Where(ele => ele.Attribute("name").Value == nodeAttributeName).FirstOrDefault(); + if (xeNode == null) + { + return string.Empty; + } + return xeNode.Element("summary").Value.Trim(); + } + /// + /// Gets the code annotation for the database table + /// + /// entity type + /// the code annotation for the database table + public string GetTableAnnotation(Type entityType) + { + return GetXElementNodeValue(entityType, $"T:{entityType.FullName}"); + } + /// + /// Gets the code annotation for the field + /// + /// entity type + /// column name + /// the code annotation for the field + public string GetPropertyAnnotation(Type entityType, string dbColumnName) + { + return GetXElementNodeValue(entityType, $"P:{entityType.FullName}.{dbColumnName}"); + } + #region Primary key private void SetColumns(EntityInfo result) { @@ -159,7 +206,7 @@ namespace SqlSugar } else { - if (sugarColumn.IsJson && String.IsNullOrEmpty(sugarColumn.ColumnDataType)) + if (sugarColumn.IsJson && String.IsNullOrEmpty(sugarColumn.ColumnDataType)) { if (this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL) { @@ -201,6 +248,7 @@ namespace SqlSugar column.ColumnDescription = sugarColumn.ColumnDescription; } } + if (column.ColumnDescription.IsNullOrEmpty()) column.ColumnDescription = GetPropertyAnnotation(result.Type, column.DbColumnName); if (this.Context.MappingColumns.HasValue()) { var golbalMappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName.Equals(result.EntityName, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName == column.PropertyName); @@ -213,13 +261,13 @@ namespace SqlSugar if (golbalMappingInfo != null) column.IsIgnore = true; } - if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityService != null) { + if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityService != null) + { this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityService(property, column); } result.Columns.Add(column); } } #endregion - } }