上一遍我介绍了如何使用引用对象的方法,这一篇我介绍一个工具,用来专门生成上一篇中各个实体类的对应部分类:不多说,直接上代码:
class Program
{
static void Main(string[] args)
{
foreach (var fileName in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory))
{
if (fileName.ToLower().EndsWith(".dll"))
{
Assembly assembly = Assembly.LoadFile(fileName);
EdmRelationshipAttribute[] atrrs = (EdmRelationshipAttribute[])Attribute.GetCustomAttributes(assembly, typeof(EdmRelationshipAttribute));
string newFoldeName = fileName.Remove(fileName.LastIndexOf('.')) + "PartialClass";
var directInfo = System.IO.Directory.CreateDirectory(newFoldeName);
//获取程序集的命名空间
string namespa = (fileName.Replace(AppDomain.CurrentDomain.BaseDirectory, ""));
namespa = namespa.Remove(namespa.LastIndexOf('.'));
foreach (var type in assembly.GetTypes())
{
if (type.IsSubclassOf(typeof(System.Data.Objects.DataClasses.EntityObject)))
{
StringBuilder strBuild = new StringBuilder();
strBuild.Append(@"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;");
strBuild.AppendLine();
strBuild.AppendLine("namespace " + namespa);
strBuild.AppendLine("{");
strBuild.AppendLine("\t#region " + type.Name);
strBuild.AppendLine("\tpublic partial class " + type.Name);
strBuild.AppendLine("\t{");
if (atrrs != null)
{
foreach (var r in atrrs)
{
if (r.Role2Type == type)
{
//添加注释
strBuild.AppendLine("\t\t/// <summary>");
strBuild.AppendLine("\t\t/// 获取" + r.Role1Type.Name);
strBuild.AppendLine("\t\t/// </summary>");
//添加方法
strBuild.AppendLine("\t\tpublic " + r.Role1Type.Name + " Get" + r.Role1Type.Name + "()");
strBuild.AppendLine("\t\t{");
strBuild.AppendLine("\t\t\tif (this." + r.Role1Type.Name + " == null)");
strBuild.AppendLine("\t\t\t\tthis." + r.Role1Type.Name + "Reference.Load();");
strBuild.AppendLine("\t\t\t return this." + r.Role1Type.Name + ";");
strBuild.AppendLine("\t\t}");
}
}
}
strBuild.AppendLine("\t}");
strBuild.AppendLine("\t#endregion");
strBuild.AppendLine();
strBuild.AppendLine("}");
using (var stream = System.IO.File.Create(System.IO.Path.Combine(directInfo.Name, type.Name + ".PartialClass.cs")))
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strBuild.ToString());
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
}
}
}
}
}
}
使用方法是,直接将实体框架的实体类项目编译成dll,然后将dll放在本工具的同级目录下就可以了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架