T4 templates to quickly generate code:
As an example of a fast raw dal file, the following is the contents of the T4 template file
<#@ Template debug="false"Hostspecific="true"Language="C #"#><#@ include file="EF. Utility.CS.ttinclude"#><#@ Output extension=". CS"#><#CodeGenerationTools Code=NewCodegenerationtools ( This); Metadataloader Loader=NewMetadataloader ( This); Coderegion Region=NewCoderegion ( This,1); Metadatatools EF=NewMetadatatools ( This);stringInputfile =@".. \\MIS. MODEL\\MIS.EDMX"; Edmitemcollection ItemCollection=Loader. Createedmitemcollection (inputfile);stringNamespaceName =code. Vsnamespacesuggestion (); Entityframeworktemplatefilemanager FileManager= Entityframeworktemplatefilemanager.create ( This);#>usingMIS. Idal;usingMIS. Model;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceMIS. dal{<#foreach(EntityType entityinchItemcollection.getitems<entitytype> (). (E =e.name)) {#> Public Partial class<#=entity. Name#>dal:basedal<<#=entity. Name#>>, I<#=entity. Name#>Dal {}<#}#>}
Description
<# C # code #>: #号中间的内容为C # code, normal C # How to write here.
<#= output value #>: Outputs a value that can be obtained in a C # code.
Outside of these are output as-is, including spaces.
When using only need to change the above template inputfile to their own, the other as the output of the existing Dal file according to their own situation as usual copy out on the line (there is no need to write their own out with the T4 template generated after deletion on the line)
The resulting code is as follows:
1 usingMIS. Idal;2 usingMIS. Model;3 usingSystem;4 usingSystem.Collections.Generic;5 usingSystem.Linq;6 usingSystem.Text;7 usingSystem.Threading.Tasks;8 9 namespaceMIS. DalTen { One Public Partial classSys_actiondal:basedal<sys_action>, Isys_actiondal A { - } - Public Partial classSys_managerdepartmentdal:basedal<sys_managerdepartment>, Isys_managerdepartmentdal the { - } - Public Partial classSys_permissiondal:basedal<sys_permission>, Isys_permissiondal - { + } - Public Partial classSys_userdal:basedal<sys_user>, Isys_userdal + { A } at}
T4 templates: Quickly generate code with T4 templates in MVC