博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework Code First反向生成代码
阅读量:6902 次
发布时间:2019-06-27

本文共 4901 字,大约阅读时间需要 16 分钟。

那些年我们生成的代码

早年,笨点的方法通常都是使用DbFirst先生成cs,然后把CS复制出来做些修改

后台基本上就自己使用T4来写,但是一直也没时间完善成通用的版本

MS官方 提供了EntityFramework PowerTools不过实在太难用

第三方的一些生成器也有好用的,不过因为持续集成的需要,所以比较少用

好吧,本文正题是推荐EntityFramework Reverse POCO Code First Generator

可以通过"扩展和更新"来安装此插件

然后对于想反向生成的项目需要做的有3件事

1.通过Nuget引用EntityFramework

2.添加一个连接字符串到App.Config或Web.Config中

3.新建项,选择"EntityFramework Reverse POCO Code First Generator"

4.生成完成

EntityFramework Reverse POCO Code First Generator的配置

EntityFramework Reverse POCO Code First Generator还是比较容易配置的,打开对应的t4文件,里面对应的就是一些配置

<#// Please make changes to the settings below.// All you have to do is save this file, and the output file(s) is/are generated. Compiling does not regenerate the file(s).// Misc settings **********************************************************************************************************************// Namespace = ""; // Override the default namespace hereDbContextName = "DataStatContext";ConnectionStringName = "MyDbContext"; // DbContext名称ConfigurationClassName = "Configuration"; // Configuration映射文件名ConfigFilenameSearchOrder = new[] { "app.config", "web.config", "app.config.transform", "web.config.transform" }; // Add more here if required. The config files are searched for in the local project first, then the whole solution second.MakeClassesPartial = true;//生成partial classGenerateSeparateFiles = true;//生成多文件UseCamelCase = true; // This will rename the tables & fields to use CamelCase. If false table & field names will be left alone.IncludeComments = true; // 包含注释IncludeExtendedPropertyComments = ExtendedPropertyCommentsStyle.AtEndOfField; //注释位置IncludeViews = false;//包含视图DisableGeographyTypes = false; //是否使用 System.Data.Entity.Spatial.DbGeometry 类型,Odata不支持此类型CollectionType = "List"; // Determines the type of collection for the Navigation Properties. "ObservableCollection" for exampleCollectionTypeNamespace = ""; // "System.Collections.ObjectModel" is required if setting the CollectionType = "ObservableCollection"AddUnitTestingDbContext = false; //是否提供单元测试Mock 类型FakeDbContext 及FakeDbSetInflector.PluralizationService = new EnglishPluralizationService(); //生成的元素包括ElementsToGenerate = Elements.Poco | Elements.Context | Elements.UnitOfWork | Elements.PocoConfiguration;// 各种命名空间PocoNamespace = "";ContextNamespace = "";UnitOfWorkNamespace = "";        PocoConfigurationNamespace = "";// Schema *****************************************************************************************************************************// If there are multiple schema, then the table name is prefixed with the schema, except for dbo.// Ie. dbo.hello will be Hello.// abc.hello will be AbcHello.// To only include a single schema, specify it below.SchemaName = null;PrependSchemaName = true; // Control if the schema name is prepended to the table name   // 黑名单或白名单TableFilterExclude = new Regex("sysdiagrams");TableFilterInclude = null;   //重命名规则*********************************************************************************************************************// Use the following function to rename tables such as tblOrders to Orders, Shipments_AB to Shipments, etc.// Example:/*TableRename = (name, schema) =>{if (name.StartsWith("tbl"))name = name.Remove(0, 3);return name.Replace("_AB", "");};*/TableRename = (name, schema) => name; // Do nothing by default// WCF ********************************************************************************************************************************// This is only intended as a helper, to get you started creating WCF contracts, and to save a lot of typing.AddWcfDataAttributes = false;ExtraWcfDataContractAttributes = ""; // This string is inserted into the [DataContract] attribute, before the closing square bracket.// Example: ""; = [DataContract]// "(Namespace = \"http://www.contoso.com\")"; = [DataContract(Namespace = "http://www.contoso.com")]// "(Namespace = Constants.ServiceNamespace)"; = [DataContract(Namespace = Constants.ServiceNamespace)]// Callbacks **********************************************************************************************************************// This method will be called right before we write the POCO header.Action
WritePocoBaseClasses = null; // t => "IMyBaseClass";// Writes any boilerplate stuffAction
WritePocoClassAttributes = t =>{// Do nothing by default// Example:// if(t.ClassName.StartsWith("Order"))// WriteLine(" [SomeAttribute]");}; // Writes optional base classesFunc
WritePocoColumn = c => c.Entity;// That's it, nothing else to configure ***********************************************************************************************// Read schemavar tables = LoadTables();// Generate outputif (tables.Count > 0){#><#@ include file="EF.Reverse.POCO.ttinclude" #><# } #>
WritePocoBaseClassBody = t =>{// Do nothing by default// Example:// WriteLine(" // " + t.ClassName);}; Func

 

   

  引用:

   

   

转载地址:http://uuldl.baihongyu.com/

你可能感兴趣的文章
yocto系统介绍
查看>>
vim退出后终端保留 退出前的内容
查看>>
Android的minSdkVersion,targetSdkVersion,maxSdkVersion
查看>>
Android 实现ActionBar定制
查看>>
mysql之子查询
查看>>
VC++ 内存泄露与检测的一种方法
查看>>
iOS 9应用开发教程之定制应用程序图标以及真机测试
查看>>
JDK7新特性<八>异步io/AIO
查看>>
RMAN正确地删除Archivelog以及设置有备库的归档删除策略
查看>>
求最长回文子串 - leetcode 5. Longest Palindromic Substring
查看>>
获取谷歌浏览器缓存视频方法
查看>>
MVC区域 视图必须派生自 WebViewPage 或 WebViewPage<TModel>
查看>>
一步一步使用ABP框架搭建正式项目系列教程
查看>>
Ubuntu14.04下如何开启Mysql远程访问
查看>>
(数学)P、NP、NPC、NP hard问题
查看>>
Java的循环结构
查看>>
Linux下的ELF可执行文件的格式解析 (转)
查看>>
Leetcode 221 Maximal Square
查看>>
do while 循环和while循环的差别
查看>>
Shell脚本:推断用户和用户组是否已经存在/创建用户和用户组
查看>>