Mapping an existing entity class (POCO) in entity Framework 4

Source: Internet
Author: User
Keywords Existing Poco selection as follows
Tags .net based class click data data model db2 developed

The full name of the Entity framework is the Ado.net Entity framework, the Ado.net based ORM (object/relational Mapping) architecture developed by Microsoft.

The main features of the Entity framework:

1. Supports multiple databases (Microsoft SQL Server, Oracle, and DB2);

2. Strong mapping engine to support stored procedures well;

3. Provide Visual Studio integration tools for visual operation;

4. Ability to integrate well with ASP.net, WPF, WCF, WCF Data services.

For more info, see: ado.net Entity Framework at-a-glance

In the blog park new Short message function development, we are ready to use the Entity Framework 4, we first faced the problem:

entity classes and databases already exist, and the class name of the entity class is the name of the table in the database, and the properties of the class are different from the corresponding field names in the table. That is, we cannot use the usual method to generate entity classes and ObjectContext through the ado.net Entity Data Model (. edmx file) in VS2010.

Note: This entity class has a salutation called Poco (Plain old CLR Objects).

The workaround is to manually configure the mapping relationship in the. edmx file.

Here is the procedure:

1. Using ado.net Entity Data Model, create a. edmx file in VS2010, as shown in the following figure:

2. According to the Entity Data Model Wizard, in the Selection Database table window, note in the Model namespace input box to enter the existing entity class namespace, as shown below:

3. Click Finish, generate the corresponding EntityType, the following figure:

The EntityType here is generated from the table structure in the database, unlike the actual entity classes, where the table name is not the same as the class name, and many of the table's fields are not the same as the properties of the class.

Here's where we configure the mapping:

4. Click on the Edmx window to click on the blank, in the Properties window in the code generation Strategy property set to None (see below), that is, do not let VS2010 automatically generate entity classes and corresponding ObjectContext.

5. Continue to the. edmx window, select the Entity class, click the right button to select Show in Model Browser, see the following figure:

6. Enter Model browser window, see the following figure:

Above, the entity class (which is VS2010 automatically generated, which corresponds to the database table one by one), is the table structure below.

Modifying the mapping is here: Change the class name to the actual entity class name in entity types, and change the property to the actual property, as shown in the following figure:

After modification, the mapping is configured, and the entity class diagram in the. edmx file is automatically changed as well, as shown in the following figure:

7. A. edmx file is actually an XML file in which the mapping information is stored. Select the file in VS2010 Solution Manager, right-click Open with, and choose to use XML Editor to see the contents of the. edmx file, as shown in the following figure:

8. Write another class that inherits from ObjectContext (see below) and the work is done.

Related Article

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.