Entity Framework-use T4 to separate entity and dbcontext

Source: Internet
Author: User

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

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.