随笔 - 33, 文章 - 0, 评论 - 148, 阅读 - 92595
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Ado.Net 实体框架学习笔记2

Posted on   ghwghw  阅读(596)  评论(0编辑  收藏  举报

上一遍我介绍了如何使用引用对象的方法,这一篇我介绍一个工具,用来专门生成上一篇中各个实体类的对应部分类:不多说,直接上代码:

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放在本工具的同级目录下就可以了

 

 

 

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示