说明:这里不解释 T4 模板的 一些基础语法,仅供参考
1 <#@ template language="C#" debug="false" hostspecific="true"#> 2 <#@ include file="EF.Utility.CS.ttinclude"#><#@ 3 output extension=".cs"#> 4 5 <# 6 7 CodeGenerationTools code = new CodeGenerationTools(this); 8 MetadataLoader loader = new MetadataLoader(this); 9 CodeRegion region = new CodeRegion(this, 1); 10 MetadataTools ef = new MetadataTools(this); 11 12 string inputFile = @"\MyFrameDBModels.edmx"; //EDMX路径 13 14 EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); 15 string namespaceName = code.VsNamespaceSuggestion(); 16 17 EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); 18 19 #> 20 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using System.Text; 26 using System.Threading.Tasks; 27 using System.Reflection; 28 29 using System.Configuration; 30 31 namespace 命名空间 32 { 33 public class 类名 34 { 35 // 代码【不变的代码】
50 <# 51 // Emit Entity Types 循环生成代码 52 foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) 53 { 54 //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs"); 55 //BeginNamespace(namespaceName, code); 56 #> 57 58 public static I<#=entity.Name#>DAL Create<#=entity.Name#>Dal() 59 { 60 var tmep = CreateInstance("<#=entity.Name#>DAL") as I<#=entity.Name#>DAL; 61 return tmep; 62 } 63 64 65 <# 66 } 67 #> 68 69 70 71 } 72 }