那些年我们生成的代码

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

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

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

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

EntityFramework Reverse POCO Code First Generator

好吧,本文正题是推荐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 here
DbContextName = "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 class
GenerateSeparateFiles = 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 example
CollectionTypeNamespace = ""; // "System.Collections.ObjectModel" is required if setting the CollectionType = "ObservableCollection"
AddUnitTestingDbContext = false; //是否提供单元测试Mock 类型FakeDbContext 及FakeDbSet Inflector.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<Table> WritePocoClassAttributes = t =>
{
// Do nothing by default
// Example:
// if(t.ClassName.StartsWith("Order"))
// WriteLine(" [SomeAttribute]");
}; // Writes optional base classes
Func<Table, string> WritePocoBaseClasses = null; // t => "IMyBaseClass"; // Writes any boilerplate stuff
Action<Table> WritePocoBaseClassBody = t =>
{
// Do nothing by default
// Example:
// WriteLine(" // " + t.ClassName);
}; Func<Column, string> WritePocoColumn = c => c.Entity;
// That's it, nothing else to configure ***********************************************************************************************
// Read schema
var tables = LoadTables();
// Generate output
if (tables.Count > )
{
#>
<#@ include file="EF.Reverse.POCO.ttinclude" #>
<# } #>

引用:

https://efreversepoco.codeplex.com/

Entity Framework Code First反向生成代码的更多相关文章

  1. Entity FrameWork Code First无法生成数据库 解决办法

    我是控制台应用程序,没有connectionStrings,试了几个方法也都不可以. 这是别人的博客用其他方法. http://www.cnblogs.com/Gyoung/archive/2013/ ...

  2. Entity Framework Code First数据库连接

    1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器->程序包管理器控制台,执行以下语句: PM> Insta ...

  3. Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  4. Entity Framework Code First关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  5. Entity Framework Code First主外键关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  6. 使用 Entity Framework Code First

    使用 Entity Framework Code First 在家闲着也是闲着,继续写我的[ASP.NET MVC 小牛之路]系列吧.在该系列的上一篇博文中,在显示书本信息列表的时候,我们是在程序代码 ...

  7. Entity Framework Code First实现乐观并发

    Entity Framework Code First实现乐观并发 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: h ...

  8. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  9. Entity Framework Code First关系映射约定【l转发】

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

随机推荐

  1. Spring 3.0 AOP (一)AOP 术语

    关于AOP.之前我已写过一个系列的随笔: <自己实现简单的AOP>,它的关注点在于实现.实现语言是C#,实现方式为 自定义实现 RealProxy 抽象类.重写Invoke方法,以便进行方 ...

  2. HtmlHelper拓展实现RadioList

    mvc中HtmlHelper可以帮助我们生成许多Html控件,但是没有类似DropDownList的RadioList,但是发现这些方法都是拓展方法,于是就想自己也拓展一个RadioList 从网上下 ...

  3. seajs的使用--主要了解模块化

    一个使用sea.js的Demo sea.js可以解决命名问题,js文件间的依赖等. index.html内容如下: <!DOCTYPE html> <html xmlns=" ...

  4. 【Win10开发】相对布局——RelativePanel控件

    我们知道,Win10引入了Universal Windows Platform,那么我们针对不同的平台该有不同的布局,此时我们就需要相对布局,就会用到RelativePanel这个控件.我们不再将控件 ...

  5. C#不对称加密

    对称加密的缺点是双方使用相同的密钥和IV进行加密.解密.由于接收方必须知道密钥和IV才能解密数据,因此发送方需要先将密钥和IV传递给接收方.这就 有一个问题,如果攻击者截获了密钥和IV,也就等于知道了 ...

  6. hdu-2063-二分图最大匹配

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. spring面试题(2)

    f-sp-1. Spring的aop你怎样实现? 用动态代理和cglib实现,有接口的用动态代理,无接口的用cglib f-sp-2. Spring在SSH起什么作用 整合作用 f-sp-3. Spr ...

  8. 【夯实PHP基础】PHP发送邮件(PHPMailer)

    本文地址 参考地址 分享提纲: 1. 概述 2. 编写代码发送邮件 3. 参考文档 1. 概述 本文是讲利用邮件类库 PHPMailer来发送邮件方法. 我们在做project的时候常常需要邮件的功能 ...

  9. 9、ASP.NET MVC入门到精通——Controller(控制器)

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 Controller主要负责响应用户的输入.主要关注的是应用程序流,输入数据的处理,以及对相关视图(View)输出数据的提供. 继承自:Sy ...

  10. 我和Ajax的故事

    我和Ajax结缘是在2015年的3月份,当时的项目需要Ajax技术来实现,但对于我来说完全是全新的名词,自己就上网上查找相关资料,结局很明显,知道概念但是具体的是什么东西根本傻傻不明白,后来这个技术是 ...