In the past development process, I will often choose the EF framework as the underlying data structure, EF provides us with a good ADO data access mechanism, he covered the data Link, LINQ and other aspects of the content, and when we use database first or code to show a clear advantage.
- Create entities with one click, eliminating the hassle of handwriting model.
- Update data structures in real time to keep the database consistent with model.
- Provides access to a variety of underlying data.
- Elegant grammar, for my favorite Lazy program Ape is a great boon.
However, the Entity Framework generates an. edmx file in VS, which causes the summary (description) to be empty and the specific bug information is "No metadata document is available." ", causing our table name to go to the field, can not predict the meaning of the field represents, which is more deadly in development, because developers can only rely on experience and speculation, the meaning of tables, fields, and not intuitive first time to know their use, to develop a lot of inconvenience.
There is a workaround on the network as follows:
Http://www.cnblogs.com/stone_w/archive/2012/10/25/2738345.html
But I tried. It's too much trouble, and once the EF is rebuilt and rebuilt.
Later I queried the information, can use the T4 template to solve this problem.
The nonsense is not verbose, start:
First we download a file getsummery, here I provide, you can directly download: Download
We create a table in the database and add a description to the fields in the table:
Then, we create a solution, select the class library
Continue, add the Entity Data Model, the concrete steps I will not demonstrate, everyone set up a database, directly generated on it.
Now let's take a look at the generated entities, and you can see that there are no field comment descriptions:
Then we unzip the downloaded files and put the extracted files into the solution:
Next, we modify app.config
, the operation is very simple, add a connection string:
12 |
< Span class= "Crayon-o" ><add name= "myconn" connectionstring = "data source=.; Initial catalog=db;user id=sa;password=xxx; " providername= " System.Data.SqlClient " /> <! -- connection with ef consistent -- |
Then we modify model.tt
and add the code at the top position:
1 |
<#@ include file= "Getsummery.ttinclude" #> |
Where to join
Immediately after you find the location of the snippet in the template <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
, insert it under code:
123 |
// <summary> ///<#= Gettablesummery (code. Escape (Entity) #>//</summary> |
Continue to find the <#=codeStringGenerator.Property(edmProperty)#>
location of the code snippet: insert it above the code:
123 |
// <summary> ///<#= Getcolumnsummery (code. Escape (entity), code. Escape (Edmproperty)) #>//</summary> |
Note: The previous comment symbol///cannot be less
Here to release a mine, convenient for everyone to control the line number, determine the approximate location:
Here, basically done, to refresh your entity model EDMX file, you will find the field comments are automatically added.
This article reprinted to: http://jeffblog.sinaapp.com/archives/501
Adding entities to the EF framework using the T4 template the ability to automatically generate field annotations from a database