创建模型Creating a Model

EF 模型可存储有关应用程序类和属性如何映射到数据库表和列的详细信息。An EF model stores the details about how application classes and properties map to database tables and columns. 创建 EF 模型有两种主要方式:There are two main ways to create an EF model:

  • 使用 Code First:开发者编写代码来指定模型。Using Code First: The developer writes code to specify the model. EF 基于实体类和开发者提供的其他模型配置,在运行时生成模型和映射。EF generates the models and mappings at runtime based on entity classes and additional model configuration provided by the developer.

  • 使用 EF 设计器:开发者使用 EF 设计器进行绘制以指定模型。Using the EF Designer: The developer draws boxes and lines to specify the model using the EF Designer. 生成的模型以 XML 格式存储在具有 EDMX 扩展名的文件中。The resulting model is stored as XML in a file with the EDMX extension. 应用程序域对象通常从概念模型中自动生成。The application's domain objects are typically generated automatically from the conceptual model.

EF 工作流EF workflows

这两种方式都可用于定位现有数据库或创建新数据库,最终会产生 4 种不同的工作流。Both of these approaches can be used to target an existing database or create a new database, resulting in 4 different workflows. 了解哪一种最适合:Find out about which one is best for you:

我只想编写代码...I just want to write code... 我想要使用设计器...I want to use a designer...
我正在创建新数据库I am creating a new database 使用 Code First 在代码中定义模型,然后生成数据库。****Use Code First to define your model in code and then generate a database. 通过 Model First 使用框和线定义模型,然后生成数据库。****Use Model First to define your model using boxes and lines and then generate a database.
我需要访问现有数据库I need to access an existing database 使用 Code First 创建映射到现有数据库的基于代码的模型。****Use Code First to create a code based model that maps to an existing database. 使用 Database First 创建映射到现有数据库的框和线模型。****Use Database First to create a boxes and lines model that maps to an existing database.

观看视频:我应该使用哪种 EF 工作流?Watch the video: What EF workflow should I use?

这段短片介绍了其中的差异,以及如何找到适合的工作流。This short video explains the differences, and how to find the one that is right for you.

主讲人Rowan MillerPresented By: Rowan Miller

Which Workflow Thumb WMV | MP4 | WMV (ZIP)Which Workflow Thumb WMV | MP4 | WMV (ZIP)

如果在观看视频后仍无法顺利决定使用 EF 设计器还是 Code First,那么分别请了解两者!If after watching the video you still don't feel comfortable deciding if you want to use the EF Designer or Code First, learn both!

查看二者的本质区别A look under the hood

无论是使用 Code First 还是 EF 设计器,EF 模型始终具有以下几个组件:Regardless of whether you use Code First or the EF Designer, an EF model always has several components:

  • 应用程序域对象或实体类型本身。The application's domain objects or entity types themselves. 这通常称为对象层This is often referred to as the object layer

  • 使用实体数据模型描述的,由特定于域的实体类型和关系组成的概念模型。A conceptual model consisting of domain-specific entity types and relationships, described using the Entity Data Model. 该层通常用字母“C”表示概念__。This layer is often referred to with the letter "C", for conceptual.

  • 表示数据库中定义的表、列和关系的存储模型。A storage model representing tables, columns and relationships as defined in the database. 该层通常用字母“S”表示存储__。This layer is often referred to with the later "S", for storage.

  • 概念模型和数据库架构之间的映射。A mapping between the conceptual model and the database schema. 该映射通常称为“C-S”映射。This mapping is often referred to as "C-S" mapping.

EF 的映射引擎利用“C-S”映射将针对实体的操作(例如创建、读取、更新和删除)转换为针对数据库中的表的等效操作。EF's mapping engine leverages the "C-S" mapping to transform operations against entities - such as create, read, update, and delete - into equivalent operations against tables in the database.

概念模型和应用程序的对象之间的映射通常称为“O-C”映射。The mapping between the conceptual model and the application's objects is often referred to as "O-C" mapping. 与“C-S”映射相比,“O-C”映射是一对一的隐式映射:概念模型中定义的实体、属性和关系需要与 .NET 对象的形状和类型相匹配。Compared to the "C-S" mapping, "O-C" mapping is implicit and one-to-one: entities, properties and relationships defined in the conceptual model are required to match the shapes and types of the .NET objects. 从 EF4 及更高版本开始,对象层可以由具有属性的简单对象组成,无需任何 EF 依赖关系。From EF4 and beyond, the objects layer can be composed of simple objects with properties without any dependencies on EF. 这些通常称为简单传统 CLR 对象 (POCO),同时类型和属性映射以名称匹配约定为基础进行。These are usually referred to as Plain-Old CLR Objects (POCO) and mapping of types and properties is performed base on name matching conventions. 以前,在 EF 3.5 中,对象层存在特定限制,例如实体必须派生自 EntityObject 类,并且必须带有 EF 属性以实现“O-C”映射。Previously, in EF 3.5 there were specific restrictions for the object layer, like entities having to derive from the EntityObject class and having to carry EF attributes to implement the "O-C" mapping.