那些年我们生成的代码

早年,笨点的方法通常都是使用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. Elasticsearch 教程--分布式集群

    集群 补充章节 正如前文提到的,这就是第个补充的章节,这里会介绍 Elasticsearch 如何在分布式环境中运行. 本章解释了常用术语,比如 集群 (cluster), 节点 (node) 以及  ...

  2. 30行代码让你理解angular依赖注入:angular 依赖注入原理

    依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...

  3. EFCore执行Sql语句的方法:FromSql与ExecuteSqlCommand

    前言 在EFCore中执行Sql语句的方法为:FromSql与ExecuteSqlCommand:在EF6中的为SqlQuery与ExecuteSqlCommand,而FromSql和SqlQuery ...

  4. 原生JS投票特效

    效果:http://hovertree.com/texiao/js/24/ 效果图: 代码如下: <!DOCTYPE html> <html lang="en"& ...

  5. Java 9将采用新的版本字符串格式

             在现有的版本编码格式使用了两年之后,从Java 9开始,Java版本方案将根据业内软件版本编码的最佳实践进行修改.使用或解析Java版本字符串的应用程序开发人员要注意了,因为这种变化 ...

  6. golang中如何使用http,socket5代理

    Golang Http use socket5 proxy 因为最近想爬取一些网站上的视频,无奈网站在墙外,只能通过代理进行爬取,因此在网上搜索关于golang使用代理的方法. 功夫不负有心人,最后我 ...

  7. [转载] php用csv文件导出大量数据

    header ( "Content-type:application/vnd.ms-excel" ); header ( "Content-Disposition:fil ...

  8. MVC数据传递

    一.数据传递 1.ViewData[]: 用法:action中:ViewData["key"]="aaa";,V层接收ViewData["key&qu ...

  9. OData V4 系列 Action 与 Function

    OData 学习目录 了解过WebApi的都知道,WebApi默认方法 Get.Post.Put,如果增加其它过多Post方法即不能识别,还需增加其它配制,请求的Url还需加上Controller及A ...

  10. 《javascript面向对象精要》读书笔记

    <javascript面向对象精要> 买这本书的原因主要是因为作者,Nicholas C. Zakas 牛X闪闪的js专家,读过js高程的应该都知道他,而这本书是他的最新力作,感觉也是js ...