【繁星Code】如何在EF將實體註釋寫入資料庫中

賣程式碼的小當家發表於2021-02-06

        最近在專案中需要把各個欄位的釋義寫到資料庫中,該專案已經上線很長時間了,資料庫中的欄位沒有上千也有上百個,要是一個專案一個專案開啟然後再去找對應欄位檢視什麼意思,估計要到明年過年了。由於專案中使用EntityFramework,本身這個專案只有手動設定欄位註釋的功能,Coder平時寫程式碼的時候都懶得寫註釋,更別提能在配置資料庫的時候將註釋配置進去,所以如何在EF中自動將實體註釋寫入資料庫,減輕Coder的壓力(ru he tou lan)尤為重要。gitee地址:https://gitee.com/lbqman/Blog20210206.git 。下面進入正題。

一、實現思路

        在FluentAPI中提供了HasComment方法,如下
 
 
 
 
 
11
 
 
 
1
    /// <summary>Configures a comment to be applied to the column</summary>
2
    /// <typeparam name="TProperty"> The type of the property being configured. </typeparam>
3
    /// <param name="propertyBuilder"> The builder for the property being configured. </param>
4
    /// <param name="comment"> The comment for the column. </param>
5
    /// <returns> The same builder instance so that multiple calls can be chained. </returns>
6
    public static PropertyBuilder<TProperty> HasComment<TProperty>(
7
      [NotNull] this PropertyBuilder<TProperty> propertyBuilder,
8
      [CanBeNull] string comment)
9
    {
10
      return (PropertyBuilder<TProperty>) propertyBuilder.HasComment(comment);
11
    }
 
 
也就是說我們要獲取到實體物件的註釋xml,然後讀取對應的欄位註釋,自動呼叫改方法即可。需要解決的問題大概有以下幾點。
  1. 如何獲取當前配置的欄位;
  2. 載入Xml,並根據欄位獲取對應的註釋;
  3. 如何將列舉的各項資訊都放入註釋中;

二、實現方法

    1.如何獲取當前配置的欄位

       在獲取xml的註釋中,需要的資訊有實體對應的型別以及對應的欄位。而包含這兩種型別的方法只有Property這個方法,該方法的出入參如下:
 
 
 
 
 
2
 
 
 
1
public virtual PropertyBuilder<TProperty> Property<TProperty>(
2
      [NotNull] Expression<Func<TEntity, TProperty>> propertyExpression);
 
 
        所以我們準備對這個方法進行改造。並且根據傳入的propertyExpression獲取欄位名稱。方法如下:
 
 
 
 
 
4
 
 
 
1
        public static PropertyBuilder<TProperty> SummaryProperty<TEntity, TProperty>(
2
            this EntityTypeBuilder<TEntity> entityTypeBuilder,
3
            Expression<Func<TEntity, TProperty>> propertyExpression)
4
            where TEntity : class
 
 
        根據表示式獲取欄位名稱如下:
 
 
 
 
 
10
 
 
 
1
        public static MemberInfo GetMember<T, TProperty>(this Expression<Func<T, TProperty>> expression)
2
        {
3
            MemberExpression memberExp;
4
            if (expression.Body is UnaryExpression unaryExpression)
5
                memberExp = unaryExpression.Operand as MemberExpression;
6
            else
7
                memberExp = expression.Body as MemberExpression;
8

9
            return memberExp?.Member;
10
        }
 
 

  2.載入Xml,並根據欄位獲取對應的註釋

    VS中的Xml格式不在此處過多解釋,屬性、方法等註釋可以根據規律去獲取。此處需要注意的是當欄位是從父類繼承而來並且父類屬於不同的dll時,需要獲取父類所在dll的xml註釋才行,另外列舉也需要同樣去處理。雖然獲取Xml資料的方法只在更新資料庫時才呼叫,但是還是需要使用字典將資料快取下來,方便下次快速獲取。具體程式碼如下:
 
 
 
 
 
204
 
 
 
1
   /// <summary>
2
    /// xml註釋獲取器
3
    /// </summary>
4
    internal static class SummaryXmlCacheProvider
5
    {
6
        #region TClass
7

8
        /// <summary>
9
        /// 根據型別初始化該類所在程式集的xml
10
        /// </summary>
11
        /// <typeparam name="TClass"></typeparam>
12
        internal static void InitSummaryXml<TClass>()
13
        {
14
            var assembly = Assembly.GetAssembly(typeof(TClass));
15
            SerializeXmlFromAssembly(assembly);
16
        }
17

18
        /// <summary>
19
        /// 根據型別獲取該類所在程式集的xml
20
        /// </summary>
21
        /// <typeparam name="TClass"></typeparam>
22
        /// <returns></returns>
23
        internal static Dictionary<string, string> GetSummaryXml<TClass>()
24
        {
25
            var assembly = Assembly.GetAssembly(typeof(TClass));
26
            return SummaryCache[assembly];
27
        }
28

29
        /// <summary>
30
        /// 獲取該類在xml的key
31
        /// </summary>
32
        /// <typeparam name="TClass"></typeparam>
33
        /// <returns></returns>
34
        internal static string GetClassTypeKey<TClass>()
35
        {
36
            return TableSummaryRuleProvider.TypeSummaryKey(typeof(TClass).FullName);
37
        }
38

39
        #endregion
40

41
        #region TProperty
42

43
        /// <summary>
44
        /// 根據型別以及欄位初始化該類所在程式集的xml
45
        /// </summary>
46
        /// <typeparam name="TClass"></typeparam>
47
        /// <typeparam name="TProperty"></typeparam>
48
        /// <param name="propertyExpression"></param>
49
        internal static void InitSummaryXml<TClass, TProperty>(Expression<Func<TClass, TProperty>> propertyExpression)
50
        {
51
            var propertyAssembly = GetPropertyAssembly(propertyExpression);
52
            SerializeXmlFromAssembly(propertyAssembly);
53
        }
54

55
        /// <summary>
56
        /// 根據型別以及欄位獲取該類所在程式集的xml
57
        /// </summary>
58
        /// <typeparam name="TClass"></typeparam>
59
        /// <typeparam name="TProperty"></typeparam>
60
        /// <param name="propertyExpression"></param>
61
        /// <returns></returns>
62
        internal static Dictionary<string, string> GetSummaryXml<TClass, TProperty>(
63
            Expression<Func<TClass, TProperty>> propertyExpression)
64
        {
65
            var propertyAssembly = GetPropertyAssembly(propertyExpression);
66
            return SummaryCache[propertyAssembly];
67
        }
68

69
        /// <summary>
70
        /// 獲取該類以及欄位所在xml的key
71
        /// </summary>
72
        /// <typeparam name="TClass"></typeparam>
73
        /// <typeparam name="TProperty"></typeparam>
74
        /// <param name="propertyExpression"></param>
75
        /// <returns></returns>
76
        internal static string GetPropertyTypeKey<TClass, TProperty>(
77
            Expression<Func<TClass, TProperty>> propertyExpression)
78
        {
79
            var memberName = propertyExpression.GetMember().Name;
80
            var propertyInfo = GetPropertyInfo(propertyExpression);
81
            var propertyKey =
82
                $"{propertyInfo.DeclaringType.Namespace}.{propertyInfo.DeclaringType.Name}.{memberName}";
83
            return PropertySummaryRuleProvider.PropertyTypeSummaryKey(propertyKey);
84
        }
85

86
        #endregion
87

88
        #region TEnum
89

90
        /// <summary>
91
        /// 獲取列舉欄位的描述資訊
92
        /// </summary>
93
        /// <typeparam name="TClass"></typeparam>
94
        /// <typeparam name="TProperty"></typeparam>
95
        /// <param name="propertyExpression"></param>
96
        /// <returns></returns>
97
        internal static string GetEnumPropertyDescription<TClass, TProperty>(Expression<Func<TClass, TProperty>> propertyExpression)
98
        {
99
            var propertyInfo = GetPropertyInfo(propertyExpression);
100
            if (!propertyInfo.PropertyType.IsEnum)
101
                return string.Empty;
102
            var enumType = propertyInfo.PropertyType;
103
            SerializeXmlFromAssembly(enumType.Assembly);
104
            var propertySummaryDic = SummaryCache[enumType.Assembly];
105
            var enumNames = enumType.GetEnumNames();
106
            var enumDescDic = enumType.GetNameAndValues();
107
            var enumSummaries = new List<string>();
108
            foreach (var enumName in enumNames)
109
            {
110
                var propertyEnumKey = PropertySummaryRuleProvider.EnumTypeSummaryKey($"{enumType.FullName}.{enumName}");
111
                var enumSummary = propertySummaryDic.ContainsKey(propertyEnumKey)
112
                    ? propertySummaryDic[propertyEnumKey]
113
                    : string.Empty;
114
                var enumValue = enumDescDic[enumName];
115
                enumSummaries.Add(PropertySummaryRuleProvider.EnumTypeSummaryFormat(enumValue,enumName,enumSummary));
116
            }
117

118
            return string.Join(";", enumSummaries);
119

120
        }
121

122
        #endregion
123

124
        /// <summary>
125
        /// 根據表示式獲取屬性所在的程式集
126
        /// </summary>
127
        /// <typeparam name="TClass"></typeparam>
128
        /// <typeparam name="TProperty"></typeparam>
129
        /// <param name="propertyExpression"></param>
130
        /// <returns></returns>
131
        private static Assembly GetPropertyAssembly<TClass, TProperty>(
132
            Expression<Func<TClass, TProperty>> propertyExpression)
133
        {
134
            var propertyInfo = GetPropertyInfo(propertyExpression);
135
            var propertyAssembly = propertyInfo.Module.Assembly;
136
            return propertyAssembly;
137
        }
138

139
        /// <summary>
140
        /// 根據表示式獲取欄位屬性
141
        /// </summary>
142
        /// <typeparam name="TClass"></typeparam>
143
        /// <typeparam name="TProperty"></typeparam>
144
        /// <param name="propertyExpression"></param>
145
        /// <returns></returns>
146
        private static PropertyInfo GetPropertyInfo<TClass, TProperty>(
147
            Expression<Func<TClass, TProperty>> propertyExpression)
148
        {
149
            var entityType = typeof(TClass);
150
            var memberName = propertyExpression.GetMember().Name;
151
            var propertyInfo = entityType.GetProperty(memberName, typeof(TProperty));
152
            if (propertyInfo == null || propertyInfo.DeclaringType == null)
153
                throw new ArgumentNullException($"this property {memberName} is not belong to {entityType.Name}");
154

155
            return propertyInfo;
156
        }
157

158
        /// <summary>
159
        /// 根據程式集初始化xml
160
        /// </summary>
161
        /// <param name="assembly"></param>
162
        private static void SerializeXmlFromAssembly(Assembly assembly)
163
        {
164
            var assemblyPath = assembly.Location;
165
            var lastIndexOf = assemblyPath.LastIndexOf(".dll", StringComparison.Ordinal);
166
            var xmlPath = assemblyPath.Remove(lastIndexOf, 4) + ".xml";
167

168
            if (SummaryCache.ContainsKey(assembly))
169
                return;
170
            var xmlDic = new Dictionary<string, string>();
171
            if (!File.Exists(xmlPath))
172
            {
173
                Console.WriteLine($"未能載入xml檔案,原因:xml檔案不存在,path:{xmlPath}");
174
                SummaryCache.Add(assembly, xmlDic);
175
                return;
176
            }
177

178
            var doc = new XmlDocument();
179
            doc.Load(xmlPath);
180
            var members = doc.SelectNodes("doc/members/member");
181
            if (members == null)
182
            {
183
                Console.WriteLine($"未能載入xml檔案,原因:doc/members/member節點不存在");
184
                SummaryCache.Add(assembly, xmlDic);
185
                return;
186
            }
187

188
            foreach (XmlElement member in members)
189
            {
190
                var name = member.Attributes["name"].InnerText.Trim();
191
                if (string.IsNullOrWhiteSpace(name))
192
                    continue;
193
                xmlDic.Add(name, member.SelectSingleNode("summary")?.InnerText.Trim());
194
            }
195

196
            SummaryCache.Add(assembly, xmlDic);
197
        }
198

199
        /// <summary>
200
        /// xml註釋快取
201
        /// </summary>
202
        private static Dictionary<Assembly, Dictionary<string, string>> SummaryCache { get; } =
203
            new Dictionary<Assembly, Dictionary<string, string>>();
204
    }
 
 

    3.如何將列舉的各項資訊都放入註釋中

  上面的兩個步驟已經根據表示式將欄位的註釋獲取到,直接呼叫EF提供的HasComment即可。見程式碼:
 
 
 
 
 
15
 
 
 
1
        public static PropertyBuilder<TProperty> SummaryProperty<TEntity, TProperty>(
2
            this EntityTypeBuilder<TEntity> entityTypeBuilder,
3
            Expression<Func<TEntity, TProperty>> propertyExpression)
4
            where TEntity : class
5
        {
6
            SummaryXmlCacheProvider.InitSummaryXml(propertyExpression);
7
            var entitySummaryDic = SummaryXmlCacheProvider.GetSummaryXml(propertyExpression);
8
            var propertyKey = SummaryXmlCacheProvider.GetPropertyTypeKey(propertyExpression);
9
            var summary = entitySummaryDic.ContainsKey(propertyKey) ? entitySummaryDic[propertyKey] : string.Empty;
10
            var enumDescription = SummaryXmlCacheProvider.GetEnumPropertyDescription(propertyExpression);
11
            summary = string.IsNullOrWhiteSpace(enumDescription) ? summary : $"{summary}:{enumDescription}";
12
            return string.IsNullOrWhiteSpace(summary)
13
                ? entityTypeBuilder.Property(propertyExpression)
14
                : entityTypeBuilder.Property(propertyExpression).HasComment(summary);
15
        }
 
 
  同時也可以設定表的註釋以及表的名稱。如下:
 
 
 
 
 
26
 
 
 
1
public static EntityTypeBuilder<TEntity> SummaryToTable<TEntity>(
2
            this EntityTypeBuilder<TEntity> entityTypeBuilder, bool hasTableComment = true,
3
            Func<string> tablePrefix = null)
4
            where TEntity : class
5
        {
6
            var tableName = GetTableName<TEntity>(tablePrefix);
7
            return hasTableComment
8
                ? entityTypeBuilder.ToTable(tableName)
9
                    .SummaryHasComment()
10
                : entityTypeBuilder.ToTable(tableName);
11
        }
12

13
        public static EntityTypeBuilder<TEntity> SummaryHasComment<TEntity>(
14
            this EntityTypeBuilder<TEntity> entityTypeBuilder) where TEntity : class
15
        {
16
            SummaryXmlCacheProvider.InitSummaryXml<TEntity>();
17
            var entityDic = SummaryXmlCacheProvider.GetSummaryXml<TEntity>();
18
            var tableKey = SummaryXmlCacheProvider.GetClassTypeKey<TEntity>();
19
            var summary = entityDic.ContainsKey(tableKey) ? entityDic[tableKey] : string.Empty;
20
            return string.IsNullOrWhiteSpace(summary) ? entityTypeBuilder : entityTypeBuilder.HasComment(summary);
21
        }
22

23
        private static string GetTableName<TEntity>(Func<string> tablePrefix)
24
        {
25
            return typeof(TEntity).Name.Replace("Entity", $"Tb{tablePrefix?.Invoke()}");
26
        }
 
 
  搞定。

三、效果展示

  執行Add-Migration InitDb即可檢視生成的程式碼。
 
 
 
 
 
 
 
 
 
 
1
    public partial class InitDb : Migration
2
    {
3
        protected override void Up(MigrationBuilder migrationBuilder)
4
        {
5
            migrationBuilder.CreateTable(
6
                name: "TbGood",
7
                columns: table => new
8
                {
9
                    Id = table.Column<long>(nullable: false, comment: "主鍵")
10
                        .Annotation("SqlServer:Identity", "1, 1"),
11
                    CreateTime = table.Column<DateTime>(nullable: false, comment: "建立時間"),
12
                    CreateName = table.Column<string>(maxLength: 64, nullable: false, comment: "建立人姓名"),
13
                    UpdateTime = table.Column<DateTime>(nullable: true, comment: "更新時間"),
14
                    UpdateName = table.Column<string>(maxLength: 64, nullable: true, comment: "更新人姓名"),
15
                    Name = table.Column<string>(maxLength: 64, nullable: false, comment: "商品名稱"),
16
                    GoodType = table.Column<int>(nullable: false, comment: "物品型別:(0,Electronic) 電子產品;(1,Clothes) 衣帽服裝;(2,Food) 食品;(3,Other) 其他物品"),
17
                    Description = table.Column<string>(maxLength: 2048, nullable: true, comment: "物品描述"),
18
                    Store = table.Column<int>(nullable: false, comment: "儲存量")
19
                },
20
                constraints: table =>
21
                {
22
                    table.PrimaryKey("PK_TbGood", x => x.Id);
23
                },
24
                comment: "商品實體類");
25

26
            migrationBuilder.CreateTable(
27
                name: "TbOrder",
28
                columns: table => new
29
                {
30
                    Id = table.Column<long>(nullable: false, comment: "主鍵")
31
                        .Annotation("SqlServer:Identity", "1, 1"),
32
                    CreateTime = table.Column<DateTime>(nullable: false, comment: "建立時間"),
33
                    CreateName = table.Column<string>(maxLength: 64, nullable: false, comment: "建立人姓名"),
34
                    UpdateTime = table.Column<DateTime>(nullable: true, comment: "更新時間"),
35
                    UpdateName = table.Column<string>(maxLength: 64, nullable: true, comment: "更新人姓名"),
36
                    GoodId = table.Column<long>(nullable: false, comment: "商品Id"),
37
                    OrderStatus = table.Column<int>(nullable: false, comment: "訂單狀態:(0,Ordered) 已下單;(1,Payed) 已付款;(2,Complete) 已付款;(3,Cancel) 已取消"),
38
                    OrderTime = table.Column<DateTime>(nullable: false, comment: "下訂單時間"),
39
                    Address = table.Column<string>(maxLength: 2048, nullable: false, comment: "訂單地址"),
40
                    UserName = table.Column<string>(maxLength: 16, nullable: false, comment: "收件人姓名"),
41
                    TotalAmount = table.Column<decimal>(nullable: false, comment: "總金額")
42
                },
43
                constraints: table =>
44
                {
45
                    table.PrimaryKey("PK_TbOrder", x => x.Id);
46
                },
47
                comment: "訂單實體類");
48
        }
49

50
        protected override void Down(MigrationBuilder migrationBuilder)
51
        {
52
            migrationBuilder.DropTable(
53
                name: "TbGood");
54

55
            migrationBuilder.DropTable(
56
                name: "TbOrder");
57
        }
58
    }
 
 

四、寫在最後

  此種方法是在我目前能想起來比較方便生成註釋的方法了,另外還想過EntityTypeBuilder中的Property方法,最後還是放棄了,因為EntityTypeBuilder是由ModelBuilder生成,而ModelBuilder又是ModelSource中的CreateModel方法產生,最後一路深扒到DbContext中,為了搞這個註釋得不償失,最後才採取了上述的方法。若是大佬有更好的方法,恭請分享。

相關文章