那些年我們生成的代碼
早年,笨點的方法通常都是使用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/