【转】EF架构~为EF DbContext生成的实体添加注释(T4模板应用)
阅读目录
相关文章系列
使用EF架构时,你的实体生成方案有多种,entity object,poco,dbcontext等等,对于entity object方案生成的实体,我们感觉很臃肿,当然它的功能很强在,但有时在查看类实体时,有些麻烦,因为所有实体都在一个类文件中,有点像linq to sql,而它的类格局也与dbml有些雷同,当然这不是今天的重点,今天主要说的是当EDMX文件添加注释后,如何把注释同时添加到dbcontext实体上。
方案:修改dbcontext的T4模版
实现:找到以下代码块
先为类加注释
string summary=string.Empty; foreach (var entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { fileManager.StartNewFile(entity.Name + ".cs"); BeginNamespace(namespaceName, code); if(entity.Documentation !=null && entity.Documentation.Summary!=null) summary=entity.Documentation.Summary; else summary=entity.Name; #> /// <summary> /// <#=summary#> /// </summary>
再为类中的属性加注释
void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty)
{}
将原来的代码删除,替换成如下代码
void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty) { if (edmProperty.Documentation != null && edmProperty.Documentation.Summary != null) { WriteProperty(Accessibility.ForProperty(edmProperty), code.Escape(edmProperty.Documentation.Summary), code.Escape(edmProperty.TypeUsage), code.Escape(edmProperty), code.SpaceAfter(Accessibility.ForGetter(edmProperty)), code.SpaceAfter(Accessibility.ForSetter(edmProperty))); } else { WriteProperty(Accessibility.ForProperty(edmProperty), code.Escape(edmProperty.Name), code.Escape(edmProperty.TypeUsage), code.Escape(edmProperty), code.SpaceAfter(Accessibility.ForGetter(edmProperty)), code.SpaceAfter(Accessibility.ForSetter(edmProperty))); } } void WriteNavigationProperty(CodeGenerationTools code, NavigationProperty navigationProperty) { var endType = code.Escape(navigationProperty.ToEndMember.GetEntityType()); if (navigationProperty.Documentation != null && navigationProperty.Documentation.Summary != null) { WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)), code.Escape(navigationProperty.Documentation.Summary), navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, code.Escape(navigationProperty), code.SpaceAfter(Accessibility.ForGetter(navigationProperty)), code.SpaceAfter(Accessibility.ForSetter(navigationProperty))); } else { WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)), code.Escape(navigationProperty.Name), navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType, code.Escape(navigationProperty), code.SpaceAfter(Accessibility.ForGetter(navigationProperty)), code.SpaceAfter(Accessibility.ForSetter(navigationProperty))); } } void WriteProperty(string accessibility, string summary, string type, string name, string getterAccessibility, string setterAccessibility) { #> /// <summary> /// <#=summary#> /// </summary> <#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; } <#+ }
保存后,它会将结果输出到与TT文件同时的cs文件中,如果模板出现错误,也会将错误信息输出到CS文件中,如果成功,就会有如何内容:
1 //------------------------------------------------------------------------------ 2 // <auto-generated> 3 // This code was generated from a template. 4 // 5 // Manual changes to this file may cause unexpected behavior in your application. 6 // Manual changes to this file will be overwritten if the code is regenerated. 7 // </auto-generated> 8 //------------------------------------------------------------------------------ 9 10 using System; 11 using System.Collections.Generic;
而且,你的模型注释信息会被同时添加到实体上,呵呵。
==================================原文链接===此文章由博客转发插件转发==================================
- 学习本是一个不断抄袭、模仿、练习、创新的过程。
- 虽然,园中已有本人无法超越的同主题博文,为什么还是要写。
- 对于自己,博文只是总结。在总结的过程发现问题,解决问题。
- 对于他人,在此过程如果还能附带帮助他人,那就再好不过了。
- 由于博主能力有限,文中可能存在描述不正确,欢迎指正、补充!
- 感谢您的阅读。如果文章对您有用,那么请轻轻点个赞,以资鼓励。
- 工控物联Q群:995475200
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)