One, nonsense
T4 (Text Template transformation Toolkit) is the code generation engine that Microsoft officially started using in VisualStudio 2008. In Visual Studio, the T4 text template is a hybrid template that consists of a number of text blocks and control logic that can generate a text file. In Visual C # or Visual Basic, the control logic is written as a fragment of program code. The resulting file can be any type of text, such as a Web page, resource file, or program source code in any language. In the current VS, as long as the scene related to code generation can basically find T4 figure, such as MVC view template, Entity framwork DataContext template and so on.
Ii. description
1. Text is based on the Visual Studio + vs Plugin (Devart T4 editor), why not Visual Studio 2015, currently Devart T4 Editor does not support vs2015, a bit of egg pain. Which master knows the production of the VS plugin, help upgrade under Devart T4 Editor chant.
2, this should be really a for the dish rookie article, if you have experience on the skip it!
Third, visual first
1. Create a new Console application (ZEJE.T4)
2, in the project "Add", "New item", Pop up the "Add New Item" dialog box, in the upper right corner of the search box enter "template", respectively, for the four kinds of templates to create new items.
3. After the file is created, the list of Solution Explorer is shown below.
4. Various questions
4.1. What is the EF 5.x DbContext generator?
4.2. What is the EF 6.x DbContext generator?
4.3. What is a text template?
4.4. What is a run-time text template?
Iv. 1. Dispel your Innocence
- Whatever it is, it's a file, or "file," or "filegroup."
- There are files with the suffix. tt
- The suffix is a. tt file, which we collectively refer to as the "T4 template file"
2, Conquer
2.1. EF 5.x DbContext Generator
When you click Add, two files are generated with a. tt suffix.
Just named "EF5", then it is to help us produce two called "XX." Context.tt "and" xx.tt "files.
Open EF5.Context.tt and Ef5.tt files, are a lot of TMD code, really do not understand the wood there? Pass it, then!
2.2. EF 6.x DbContext Generator
What a TMD! Exactly the same as 2.1 said, or a bunch of TMD code. However, careful will find that the contents of the document will be different. Is it a pass? No, take a look.
File EF6.Context.cs and EF6.cs inside basically no content, go back to see EF5.Context.cs and EF5.cs file, basically the same is no content, pass it!
2.3. Text template
Open Texttemplate_.tt, the front of a bright wood has, this is how big gap!
The content in this texttemplate_.tt file is so simple that everything in the Texttemplate_.txt file is wood.
Why is there a. tt file that is XXX.cs file and XXX.txt file? Look back carefully under the original is this code (such as) in mischief
2.4. Run-time text file
Open the files for runtimetexttemplate_.tt and Runtimetexttemplate_.cs, respectively, if they are the same as above. tt file a bunch of code and expanded file basically no content? All right! Originally thought that he found the law, but found himself too naïve,. tt file content is very small, and the contents of the. cs file are a bunch!
V. Compounding the difficulties
Why is it worse? Because the following is also a T4 template with a little relationship!
0. New project "Zeje.ef_" (Project type is Class library, the. NET Framework version is 4.5)
1, in the project "Add", "New item", Pop up the "Add New Item" dialog box, in the upper right corner of the search box enter "Model", create the ADO Entity Data Model "zeje.hr"
2, click Add, found a choice question, in the end choose Which?
- EF Designer from the database
- Empty EF Designer Model
- Empty Code First Model
- Code first from the database
Well, let the storm come more violently! We create four, respectively named, compared (the specific new process is not listed to occupy space)!
- EF Designer from Database (named: Dbfirst, database based on its own local [zeje.hr] database)
- Empty EF Designer Model (named: dbfirst_)
- Empty Code First model (named: Codefirst_,)
- Code first from the database (named: Codefirst, database based on its own local [zeje.hr] database "Put the correlation in the Codefirst folder")
All right! Dare not play with me ruthless, only the first kind of T4 template with the relationship. Then the picture is quoted, the relevant T4 template file has been circled with a red box, explain it!
- DBFIRST.EDMX "is essentially an XML file that records a database-mapped entity, which includes tables, views, stored procedures, constraints, and so on"
- DBFirst.Context.tt "T4 template, expanded with a DBFirst.Designer.cs file below"
- DBFirst.Designer.cs, "I don't know what's going on here."
- DBFirst.edmx.diagram "is essentially an XML file that records the coordinates of entities under the EDMX Design view"
- Dbfirst.tt "T4 template, expanded below there are also more. cs files, TMD It's the only one. cs or a. txt file differs from the above, as far as how the whole back is studied together"
For these files, they are a filegroup, their layout depends on the "Zeje.EF_.csproj" file, if opened with Notepad, its essence is an XML file, through a certain rule, the above file is organized together. In the daily operation, we may accidentally drag the next one of these files, it's a combination of chaos, at that time can only be manually adjusted. csproj files, re-set according to established rules.
T4 Template dish Beginner's article