MVC4+EF5 EDMX Code Analysis

Source: Internet
Author: User

This article analyzes the model file code (the extension EDMX) that the entity Framework (EF) automatically generates from the database.

I. Overview

This article uses the database structure as simple as possible, only 2 tables, a user table and a branch table (equivalent to the Department table), a user must belong to a branch, so the user table has a foreign key associated to the branch table. Data (entity) Model 1.

(Figure 1 Data model)

Second, the overall structure

Open the. edmx file as text, and you can see that this is an XML file that contains 2 parts under the root node, as shown in 2.

(Figure 2 The entire document consists of 2 large parts)

Figure 2, the first part of the <edmx:Runtime> relationship to automatically generate entity classes, mappings, attributes and other CS code, is the next focus on the analysis of the content. The <Designer> part is used by the designer and contains information such as the layout of the diagram, which is irrelevant to the topic of this article and is ignored.

The next step is to analyze the runtime node and expand this node to see that it includes 3 parts, the storage model (or physical model) Storagemodels, the conceptual model Conceptualmodels, and the mapping mappings,3.

(Figure 3 The 3 major components of runtime)

Let's analyze each of the three parts of the runtime.

2.1. Physical Model Storagemodels

Storagemodel There is only one sub-node schema. Below the schema are 3 parts, namely entity container EntityContainer, entity type EntityType (each entity generates one such node), Relationship association (each of the database's foreign key relationships generates one such node). As shown in 4.

(Figure 4 Runtime.storagemodel content)

The contents of 2.1.1 and EntityContainer are as follows.

(Figure 5 Runtime.StorageModel.EntityContainer content)

As you can see from Figure 5, EntityContainer lists the entities and relationships that are included in the storage model.

2.1.2, and then look at the contents of EntityType node. This node corresponds to a database table, which corresponds to an entity. How many tables there are in the database, and how many such nodes are generated. The following is an example of systemuser (because the table has a foreign key) to see the contents of the node, as shown in 6.

(Fig. 6 Runtime.StorageMode.EntityType node content)

As you can see, the main content of the EntityType node is to list the fields in the table and does not reflect the foreign key relationship. A foreign key relationship is represented in the later section of the analysis.

2.1.3, and then look at association node content. This node corresponds to a foreign key relationship of the database, and each foreign key generates a association node. In this example, the foreign key is the user to the branch, the node content 7 is shown.

(Fig. 7 Runtime.StorageModel.Association node content)

As can be seen from Figure 7, the association node first defines the two sides of the foreign key relationship as well as the number of re-keys, and then defines the primary key table, column and foreign key table, columns.

So far, the physical model Storagemodel has been analyzed.

2.2. Conceptual Model Conceptualmodels

For an automatically generated entity model, the conceptual model is consistent with the content of the physical model. The conceptual model is shown in structure 8.

(Figure 8 Conceptual model Runtime.conceptualmodels structure)

As can be seen from Figure 8, the runtime.conceptualmodels structure and the runtime.storagemodels structure is exactly the same, if the expansion of the various sub-nodes will be seen, the internal structure is consistent, there is no longer analysis.

2.3. Mapping Mappings

Mapping refers to the mapping of a physical model to a conceptual model. This section is relatively simple, there is a entitycontainermapping node below the Mappings node, and a node entitysetmapping is generated for each entity below, where each column (attribute) is mapped. As shown in 9.

(Figure 9 Mapping runtime.mappings structure)

This article was modified from: http://www.cnblogs.com/FoundationSoft/archive/2011/01/08/1930479.html

MVC4+EF5 EDMX Code Analysis

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.