Original article address:
Http://www.cnblogs.com/fangrobert/archive/2011/08/22/2150048.html
In general, we will create a new ef. data layer of classlibrary in the project, and then add an edmx file of ADO. NET Entity Data Model to the layer. IDE uses ADO. NET Entity Data Model to generate data access based on the corresponding object classes and object classes of the current database.Code. If your project is a simple three-tier architecture (application layer, business layer, data access layer), you will find that we need to use entity classes at the application layer, you must reference EF. in this way, the data access code is exposed to the application layer. This is contrary to the original intention of our three-tier architecture.
Let's use step by step to separate entity and dbcontext with T4.
Step 1 create Solution
Ef. app application layer-Console Application
Ef. Data entity layer-classlibrary
Ef. dataaccess data access layer-classlibary
Ef. services business layer-classlibary
Step 2 add edmx
Open efmodel. edmx file, right-click the menu and select "Add code generation item". In the displayed window, select "ADO. net dbcontext generator ", EF. two T4 files are generated in dataaccess project: efmodel. TT, efmodel. context. TT
I think you should have a hunch that efmodel. Tt is a T4 template file produced to generate entities, while efmodel. Context. Tt is a T4 template file produced to generate dbcontext.
Step 3 Transfer entity
Set efmodel. edmx and efmodel. TT to EF. data, and then EF. delete efmodel in dataaccess. TT, and then set efmodel. edmx code generation strategy is set to none [Ensure efmodel. edmx does not automatically generate code]
Step 4 correct reference
APP references data and service
Service references data
Dataaccess references data
In this way, the app and dataaccess are isolated by service, which is our purpose.
We can modify the T4 template file, so we can make some modifications to the newly generated two T4 files to produce the code we want. For example, you can modify the efmodel. tt file to add the produced entity class to datacontract to meet the requirements of applications in WCF.
Download demo