Ef+t4 template Implementation Code Quick Generator

Source: Internet
Author: User

Reprint Please specify address: http://www.cnblogs.com/cainiaodage/p/4953601.html

Effect, demo, but also hope that everyone's generous enlighten. (Click the demo download case)

Project structure

T4BLL add bll.tt file;

T4model add model file;

T4dal add dal.tt file;

T4dal add the ADO Entity Data model to select Dababase First mode;

Open Model1.tt Open under the model1.edmx file, the copied content replaces the Model.tt file under the T4model project;

Open Model.tt under Model1.edmx file from <#=codestringgenerator.entityclassopening (entity) #>{ Select to } <# Endnamespace (code); Delete

Such as

Copy model1.tt content replace Bll.tt, dal.tt content, now Model1.tt has no meaning, can be deleted.

Open the T4model T4 template, modify the following code, after the modification is complete, you can generate the model of the Add class in the EDMX.

Const string @" MODEL1.EDMX " ; // Change to string inputfile = host.resolveassemblyreference ("$ (projectdir)"). Replace ("t4model","t4dal") +"  /MODEL1.EDMX"

Open the T4dal T4 template

Filemanager.startnewfile (entity. Name +". CS");//Change toFilemanager.startnewfile (entity. Name +"_dal.cs");//---split line---  Public stringentityclassopening (EntityType entity) {return string. Format (CultureInfo.InvariantCulture,"{0} {1}partial class {2}{3}", Accessibility.fortype (entity), _code. SpaceAfter (_code. Abstractoption (entity)), _code. Escape (entity), _code. Stringbefore (" : ", _typemapper.gettypename (entity.    (BaseType))); }//Change to  Public stringentityclassopening (EntityType entity) {return string. Format (CultureInfo.InvariantCulture,"{0} {1}partial class {2}_dal{3}", Accessibility.fortype (entity), _code. SpaceAfter (_code. Abstractoption (entity)), _code. Escape (entity), _code. Stringbefore (" : ", _typemapper.gettypename (entity.    (BaseType))); }//---Split line---change toforeach(varEntityinchTypemapper.getitemstogenerate<entitytype>(ItemCollection)) {Filemanager.startnewfile (entity. Name+"_dal.cs"); Beginnamespace (code); #>usingT4model;<#=codestringgenerator.usingdirectives (Inheader:false) #><#=codestringgenerator.entityclassopening (Entity) #>{    /// <summary>    ///New/// </summary>    /// <param name= "entity" ></param>    /// <returns></returns>     Public<#= entity. Name #> Add (<#= entity. Name #>entity) {        //Content Implementation        return NULL; }}<# Endnamespace (code);}

Open the T4BLL T4 template

Const stringInputfile =@"MODEL1.EDMX";//Change tostringInputfile = Host.resolveassemblyreference ("$ (ProjectDir)"). Replace ("T4BLL","T4dal")+"/MODEL1.EDMX"; //---split line---Filemanager.startnewfile (entity. Name +". CS");//Change toFilemanager.startnewfile (entity. Name +"_bll.cs");//---split line---  Public stringentityclassopening (EntityType entity) {return string. Format (CultureInfo.InvariantCulture,"{0} {1}partial class {2}{3}", Accessibility.fortype (entity), _code. SpaceAfter (_code. Abstractoption (entity)), _code. Escape (entity), _code. Stringbefore (" : ", _typemapper.gettypename (entity.    (BaseType))); }//Change to  Public stringentityclassopening (EntityType entity) {return string. Format (CultureInfo.InvariantCulture,"{0} {1}partial class {2}_bll{3}", Accessibility.fortype (entity), _code. SpaceAfter (_code. Abstractoption (entity)), _code. Escape (entity), _code. Stringbefore (" : ", _typemapper.gettypename (entity.    (BaseType))); }//---Split line---moreforeach(varEntityinchTypemapper.getitemstogenerate<entitytype>(ItemCollection)) {Filemanager.startnewfile (entity. Name+"_bll.cs"); Beginnamespace (code); #>usingT4dal;usingT4model;<#=codestringgenerator.usingdirectives (Inheader:false) #><#=codestringgenerator.entityclassopening (Entity) #>{    <#= entity. Name #>_dal DAL =New<#= entity. Name #>_dal (); /// <summary>    ///New/// </summary>    /// <param name= "entity" ></param>    /// <returns></returns>     Public<#= entity. Name #> Add (<#= entity. Name #>entity) {        returndal.    ADD (entity); }}<# Endnamespace (code);}

Host.resolveassemblyreference ("$ (ProjectDir)") is the way to get the project path, by replacing the project name to find the edmx file, which enables the T4 template to be detached from the different class libraries.

Ef+t4 template Implementation Code Quick Generator

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.