那些年我们生成的代码

早年,笨点的方法通常都是使用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. 利用Swashbuckle生成Web API Help Pages

    利用Swashbuckle生成Web API Help Pages 这系列文章是参考了.NET Core文档和源码,可能有人要问,直接看官方的英文文档不就可以了吗,为什么还要写这些文章呢? 原因如下: ...

  2. SOA、ESB、NServiceBus、云计算 总结

    SOA SOA 是通过功能组件化.服务化,来实现系统集成.解决信息孤岛,这是其主要目标.而更进一步则是实现更快响应业务的变化.更快推出新的应用系统.与此同时,SOA 还实现了整合资源,资源复用. SO ...

  3. [转]C#中的string.Format()的JS版本

    String.prototype.format = function (args) { var result = this; if (arguments.length > 0) { var re ...

  4. SQL Server:APPLY表运算符

    SQL Server 2005(含)以上版本,新增了APPLY表运算,为我们日常查询带来了极大的方便. 新增的APPLY表运算符把右表表达式应用到左表表达式中的每一行.它不像JOIN那样先计算那个表表 ...

  5. Unity3D 5.x 简单实例 - 孤岛场景搭建

    1,如果我们的Unity 5.X 是从这个地址下载的,安装后不会有资源包,则需要从Unity3D官网下载资源包: Standard Assets (Unity 5.x) 下载 下载后安装后,Unity ...

  6. ABP中使用Redis Cache(2)

    上一篇讲解了如何在ABP中使用Redis Cache,虽然能够正常的访问Redis,但是Redis里的信息无法同步更新.本文将讲解如何实现Redis Cache与实体同步更新.要实现数据的同步更新,我 ...

  7. 哈哈:sqlserver2008附加数据库时操作系统错误5(拒绝访问)错误5012的解决办法

    老师说:无论干什么,出错了,先大笑三声.如果人多了,在心里也要大笑三声.哈哈哈!!! 刚刚重装系统完后,然后想学习.自己去安装sqlserver2008数据库,完之后想附加之前的数据库.可是当我一点确 ...

  8. node学习笔记

    一.准备(github地址) 什么是Javascript? ... Javascript能做什么? ..... 浏览器中的Javascript可以做什么? 操作DOM(增删改查) AJAX/跨域 BO ...

  9. 关于webStrom-11.1配置less且自动生成.css和自动压缩为.min.css/.min.js

    网上看过很多配置思路,自己总结了以下, 就把我个人配置的顺序以及材料分享下,webstrom以下简称WB 1.配置less需要安装nodejs,自行安装.因为要用到npm.我是直接把npm解压到C盘根 ...

  10. border-radius结合transition的一个小应用(动画)

    <head lang="en"> <meta charset="UTF-8"> <title></title> ...