T4 templates: Quickly generate code with T4 templates in MVC

Source: Internet
Author: User

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#>&gt, 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.