Entity Framework Code First反向生成代码

那些年我们生成的代码

早年,笨点的方法通常都是使用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 > 0)
{
#>
<#@ include file="EF.Reverse.POCO.ttinclude" #>
<# } #>

 

   

  引用:

https://efreversepoco.codeplex.com/

   

   

转载于:https://www.cnblogs.com/chsword/p/EntityFramework_Reverse_POCO_Code_First_Generator.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值