Use a POCO entity generator based on T4 templates in EF

Source: Internet
Author: User

In Entity Framework 4.0, Poco (simple traditional CLR object/plain old CLR object) is supported, so that we can focus on business logic and perform unit tests more effectively. poco does not need to inherit from the default EF entity, so it can be switched more flexibly.

This article describes how to automatically generate poco and replace the entity generated by EF to separate the two.

Download and install the POCO entity generator

This document uses the T4 template provided by Microsoft ,:

Http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313/view/Discussions/all

You can also click here to download it locally. (This article uses version 1.1 updated on.) After the download is complete, install it.

 

Use POCO entity generator

1. This article uses the northwind sample database. Because Poco is used as an entity, You need to disable the EF built-in model generator.

2. Use POCO to generate an object. Right-click the edmx blank space to add it (you can also manually add it, but you need to modify the template file so that it corresponds to edmx .)

3. Click to open the page:

4. Generate a solution:

5. the console program is created in this project. Now you can try to Use POCO to get data!

6. Running effect:


So far, the original entity generator of EF is replaced with Poco.

However, this alone is not enough, and fundamental decoupling should be achieved.

Project Separation

Create a basic class, add EF, and Use POCO

Create an object and move the POCO object of the base class to the object class.

After the modification, the compilation fails ,:

This is because the corresponding entity is not found (the entity cannot be found)

Solution:

1. Modify northwind. Context. tt, add using to reference using pocodemo. Entities, and add the reference relationship of the Response Project.

2. Modify row 22nd of northwind. tt string inputfile = @ "northwind. edmx ";

Change it to string inputfile = @ ".. \ pocodemo. Infrastructure \ northwind. edmx ";

In this way, the edmx file can be correctly found to generate code.

The final running effect is the same as before.

Postscript

This T4 template can be adjusted based on your project needs.

If edmx changes in the future, you only need to click the TT file to save and the generated code will be automatically refreshed.

Download the demo source code

(PS. I wrote a technical article for the first time. Please click it)

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.