Entity Framework file automatically generated code analysis model edmx

edmx code analysis

This paper analyzes Entity Framework generated automatically from a database model file code (extension EDMX ).

1. Overview

As used herein, the database structure as simple as possible, only 2 table, a user table and a division table (corresponding to the department table), a user must belong to a branch, the user table has a foreign key to the branch table. Data (solid) model shown in Figure 1 .

FIG 1   Data Model

2. The overall structure

Open text .edmx file, which can be seen a xml file, the root node includes 2 part, as shown in FIG 2 FIG.

Figure 2   entire document consists of two major components

FIG. 2 , the first portion is <edmx: Runtime> Relationship to automatically generate the entity class, mapping, and other properties cs code, the next thing to focus analysis. <Designer> section is designed for use, contains the information layout map, irrespective of the subject matter described herein, it is ignored.

Next focuses on Runtime node, expand the node can be seen include three parts, the storage model (or physical model) StorageModels , conceptual model ConceptualModels and mapping the Mappings , FIG. 3 shown in FIG.

FIG 3   the Runtime of 3 large part

Let's analyze individually Runtime these three parts.

3. The physical model StorageModels

StorageModel following only one child node the Schema . In the Schema is included below 3 parts of, respectively, a container is an entity the EntityContainer , entity type the EntityType (each entity generates such a node), the relationship Association or (each foreign key generates a relational database such node). FIG 4 FIG.

Figure 4   Runtime.StorageModel content

EntityContainer inside follows.

Figure 5   Runtime.StorageModel.EntityContainer content

From Fig 5 it can be seen, the EntityContainer list of entities and relationships included in the model storage.

Look EntityType content node. The nodes correspond to a database table, corresponding to an entity. Number of tables in the database, the number of such nodes is generated. Below SystemUser example (because the foreign key table) to see the contents of the node, as shown in 6 in FIG.

FIG . 6   Runtime.StorageMode.EntityType node content

As can be seen from the figure, the EntityType node main content is a table that lists each field, and does not reflect the foreign key relationship. Foreign key relationship is content to be analyzed later represented.

Next, look Association node content. This node corresponds to a foreign key in the relational database, each generates a foreign key Association node. In this example, the foreign key for the user to branch, the contents of the node in FIG. 7 FIG.

FIG . 7   Runtime.StorageModel.Association node content

从图 7 可以看出, Association 结点首先定义了外键关系的双方以及重数,接下来定义了主键表、列和外键表、列。

到此为止,物理模型 StorageModel 已经分析完毕。

4. 概念模型 ConceptualModels

对于自动生成的实体模型来说,概念模型与物理模型的内容是一致的。概念模型结构如图 8 所示。

8  概念模型 Runtime.ConceptualModels 结构

从图 8 可以看出, Runtime.ConceptualModels 结构与 Runtime.StorageModels 结构是完全一致的,如果展开各个子结点就会看到,内部的结构也是一致的,在此就不再一一分析。

5. 映射 Mappings

映射指的是物理模型到概念模型的映射。这部分内容比较简单, Mappings 结点下面有一个 EntityContainerMapping 结点,再下面对每个实体生成一个结点 EntitySetMapping ,其中对每一列(属性)都进行了映射。如图 9 所示。

9  映射 Runtime.Mappings 结构


版权所有:基础软件。作者邮箱:[email protected]。本文首发于 http://www.cnblogs.com/FoundationSoft。文章转载请保持此版权信息并注明出处。

发布了14 篇原创文章 · 获赞 4 · 访问量 2万+

Guess you like

Origin blog.csdn.net/greatwall_sdut/article/details/6123918