codefirst 根据代码注释,迁移到数据库备注

This commit is contained in:
28810
2019-06-14 18:14:14 +08:00
parent 38d51a809d
commit 5ce51bc310
9 changed files with 452 additions and 0 deletions

View File

@ -2270,6 +2270,13 @@
<param name="database"></param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.CommonUtils.GetProperyCommentBySummary(System.Type)">
<summary>
通过属性的注释文本,通过 xml 读取
</summary>
<param name="type"></param>
<returns>Dictkey=属性名value=注释</returns>
</member>
<member name="P:FreeSql.Internal.Model.TableRef.RefMiddleEntityType">
<summary>
中间表,多对多

View File

@ -8,11 +8,14 @@ using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
namespace FreeSql.Internal {
public abstract class CommonUtils {
@ -239,6 +242,36 @@ namespace FreeSql.Internal {
return iidx == 1 ? sb.Remove(0, 5).Remove(sb.Length - 1, 1).ToString() : sb.Remove(0, 4).ToString();
}
/// <summary>
/// 通过属性的注释文本,通过 xml 读取
/// </summary>
/// <param name="type"></param>
/// <returns>Dictkey=属性名value=注释</returns>
public static Dictionary<string, string> GetProperyCommentBySummary(Type type) {
var xmlPath = type.Assembly.Location.Replace(".dll", ".xml");
if (File.Exists(xmlPath) == false) return null;
var dic = new Dictionary<string, string>();
var className = type.IsNested ? $"{type.Namespace}.{type.DeclaringType.Name}.{type.Name}" : $"{type.Namespace}.{type.Name}";
var sReader = new StringReader(File.ReadAllText(xmlPath));
using (var xmlReader = XmlReader.Create(sReader)) {
var xpath = new XPathDocument(xmlReader);
var xmlNav = xpath.CreateNavigator();
var props = type.GetProperties();
foreach (var prop in props) {
var node = xmlNav.SelectSingleNode($"/doc/members/member[@name='P:{className}.{prop.Name}']/summary");
if (node == null) continue;
var comment = node.InnerXml.Trim(' ', '\r', '\n', '\t');
if (string.IsNullOrEmpty(comment)) continue;
dic.Add(prop.Name, comment);
}
}
return dic;
}
public static void PrevReheatConnectionPool(ObjectPool<DbConnection> pool, int minPoolSize) {
if (minPoolSize <= 0) minPoolSize = Math.Min(5, pool.Policy.PoolSize);
if (minPoolSize > pool.Policy.PoolSize) minPoolSize = pool.Policy.PoolSize;

View File

@ -10,6 +10,7 @@ namespace FreeSql.Internal.Model {
public string CsName { get; set; }
public Type CsType { get; set; }
public ColumnAttribute Attribute { get; set; }
public string Comment { get; internal set; }
static ConcurrentDictionary<ColumnInfo, Func<object, object>> _dicGetMapValue = new ConcurrentDictionary<ColumnInfo, Func<object, object>>();
public object GetMapValue(object obj) {

View File

@ -56,6 +56,7 @@ namespace FreeSql.Internal {
if (tbattr != null) trytb.DisableSyncStructure = tbattr.DisableSyncStructure;
var propsLazy = new List<(PropertyInfo, bool, bool)>();
var propsNavObjs = new List<PropertyInfo>();
var propsComment = CommonUtils.GetProperyCommentBySummary(entity);
foreach (var p in trytb.Properties.Values) {
var setMethod = trytb.Type.GetMethod($"set_{p.Name}");
var colattr = common.GetEntityColumnAttribute(entity, p);
@ -124,6 +125,8 @@ namespace FreeSql.Internal {
CsType = p.PropertyType,
Attribute = colattr
};
if (propsComment != null && propsComment.TryGetValue(p.Name, out var trycomment))
col.Comment = trycomment;
if (colattr.IsIgnore) {
trytb.ColumnsByCsIgnore.Add(p.Name, col);
continue;