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/