In the Entity Framework 4 mapping existing Entity class (POCO)

By Brandon West,2015-02-17 00:08
39 views 0
In the Entity Framework 4 mapping existing Entity class (POCO)

    In the Entity Framework 4 mapping existing Entity

    class (POCO)

    ADO.NET Entity Framework, is the full name of the Entity Framework is developed by Microsoft ADO.NET based ORM (Object/Relational Mapping) Framework.

    The main characteristics of the Entity Framework:

    1. Support for multiple database (Microsoft SQL Server, Oracle, and DB2);

    2. Strong mapping engine, can well support stored procedures;

    3. Provide Visual Studio integration tool for visualization operation;

    4. Ability to interact with ASP.NET, WPF, WCF, WCF Data Services for good integration.

    More introduction, please see:ADO.NET Entity Framework At-a-Glance

    In the blog the new short message function development, we are prepared to use the Entity Framework 4, the first thing we face such a problem:

    Entity class with the database have existed, and the class name of the entity class with the name of the table in the database, the attributes of a class is different from the corresponding field name in the table.That is to say we can't use the usual method in VS2010 through ADO.NET Entity Data Model (. Edmx file) generated Entity class and ObjectContext.

    Note: this entity class has a name called POCO (Plain Old CLR Objects).

    Our solution is: in. Edmx file manually configure mapping relation.

    Below is the steps:

    1. Use ADO.NET Entity Data Model, create a on the VS2010. Edmx file, the following figure:

2. According to the Entity Data Model Wizard, when selecting a database table

window, pay attention to in the Model the Namespace in the input box input current

Entity class Namespace, the diagram below:

3. Click Finish to generate the corresponding EntityType, the diagram below:

    EntityType here is generated according to the table in the database structure, and the actual entity class is different, is not the same as the table name and the name of the class, the table of many different fields and attributes of a class.

    Below we configure the mapping of key areas:

    4. Click. Edmx window click on the blank space, Code Generation in the properties window of the Strategy attribute is set to None (see below), also is not let VS2010 automatically generate entity class and corresponding ObjectContext.

5. Continue to. Edmx window, select the entity class, right click to select

the Show in the Model Browser, as shown in the figure below:

6. Enter the Model Browser window, see below:

    Above, the above is the entity class (here is VS2010 automatically generated, and the database table is one-to-one), below is the table structure.

    Modify the mapping is: here in the Entity Types will be the name of the class to the actual Entity class name, attributes to the actual attributes, the diagram below:

After modification, the mapping is configured, the edmx file entity class

diagram also will automatically change, see below:

    7.. the edmx files is an XML file, the mapping information is stored in them.In VS2010 solution manager select the file, right-click the Open With, choose Open With XML Editor, you can see. The content of the edmx file, the following figure:

Report this document

For any questions or suggestions please email