关系-EF 设计器Relationships - EF Designer

备注

本页提供有关使用 EF 设计器设置模型中的关系的信息。This page provides information about setting up relationships in your model using the EF Designer. 有关 EF 中的关系以及如何使用关系访问和操作数据的一般信息,请参阅 关系 & 导航属性For general information about relationships in EF and how to access and manipulate data using relationships, see Relationships & Navigation Properties.

关联定义模型中的实体类型之间的关系。Associations define relationships between entity types in a model. 本主题说明如何将关联与 Entity Framework Designer (EF 设计器) 进行映射。This topic shows how to map associations with the Entity Framework Designer (EF Designer). 下图显示了在使用 EF 设计器时使用的主窗口。The following image shows the main windows that are used when working with the EF Designer.

EF 设计器

备注

在生成概念模型时,有关未映射的实体和关联的警告可能会显示在“错误列表”中。When you build the conceptual model, warnings about unmapped entities and associations may appear in the Error List. 您可以忽略这些警告,因为在您选择从模型生成数据库后,错误将消失。You can ignore these warnings because after you choose to generate the database from the model, the errors will go away.

关联概述Associations Overview

使用 EF 设计器设计模型时,.edmx 文件表示您的模型。When you design your model using the EF Designer, an .edmx file represents your model. 在 .edmx 文件中, Association 元素定义了两个实体类型之间的关系。In the .edmx file, an Association element defines a relationship between two entity types. 关联必须指定关系中涉及的实体类型和关系的每一端可能的实体类型数量(也称为重数)。An association must specify the entity types that are involved in the relationship and the possible number of entity types at each end of the relationship, which is known as the multiplicity. 关联端的多重性的 (值可以是 1) 、零或一个 (0 .. 1) 或多个 (*) 。The multiplicity of an association end can have a value of one (1), zero or one (0..1), or many (*). 此信息是在两个子 结束 元素中指定的。This information is specified in two child End elements.

在运行时,如果选择在实体) 中公开外键,则可以通过导航属性或外键访问关联一端的实体类型实例 (。At run time, entity type instances at one end of an association can be accessed through navigation properties or foreign keys (if you choose to expose foreign keys in your entities). 公开外键后,实体之间的关系将使用 ReferentialConstraint 元素进行管理, (关联 元素) 的子元素。With foreign keys exposed, the relationship between the entities is managed with a ReferentialConstraint element (a child element of the Association element). 建议你始终公开实体中关系的外键。It is recommended that you always expose foreign keys for relationships in your entities.

备注

在多对多 (* : *) 不能将外键添加到实体。In many-to-many (*:*) you cannot add foreign keys to the entities. 在 * : * 关系中,使用独立的对象管理关联信息。In a *:* relationship, the association information is managed with an independent object.

有关 CSDL 元素 (ReferentialConstraintAssociation等 ) 的信息,请参阅 csdl 规范For information about CSDL elements (ReferentialConstraint, Association, etc.) see the CSDL specification.

创建和删除关联Create and Delete Associations

使用 EF 设计器创建关联会更新 .edmx 文件的模型内容。Creating an association with the EF Designer updates the model content of the .edmx file. 创建关联后,您必须为本主题后面讨论的关联 (创建映射) 。After creating an association, you must create the mappings for the association (discussed later in this topic).

备注

本部分假定已添加要在模型之间创建关联的实体。This section assumes that you already added the entities you wish to create an association between to your model.

创建关联To create an association

  1. 右键单击设计图面的空白区域,指向 " 添加新项",然后选择 " 关联 ..."。Right-click an empty area of the design surface, point to Add New, and select Association….

  2. 在 " 添加关联 " 对话框中填写关联的设置。Fill in the settings for the association in the Add Association dialog.

    添加关联

    备注

    您可以通过清除 **导航属性 **并 **将外键属性添加到 " < 实体类型名称 > 实体" **复选框,选择不向关联端的实体添加导航属性或外键属性。You can choose to not add navigation properties or foreign key properties to the entities at the ends of the association by clearing the **Navigation Property **and **Add foreign key properties to the <entity type name> Entity **checkboxes. 如果只添加一个导航属性,则将只能在一个方向遍历关联。If you add only one navigation property, the association will be traversable in only one direction. 如果不添加导航属性,则必须选择添加外键属性才能访问位于关联各端的实体。If you add no navigation properties, you must choose to add foreign key properties in order to access entities at the ends of the association.

  3. 单击 "确定"Click OK.

删除关联To delete an association

若要删除关联,请执行以下操作之一:To delete an association do one of the following:

  • 右键单击 EF 设计器图面上的关联,然后选择 " 删除"。Right-click the association on the EF Designer surface and select Delete.

  • 或 -OR -

  • 选择一个或多个关联并按 Delete 键。Select one or more associations and press the DELETE key.

将外键属性包含在实体中 (引用约束) Include Foreign Key Properties in Your Entities (Referential Constraints)

建议你始终公开实体中关系的外键。It is recommended that you always expose foreign keys for relationships in your entities. 实体框架使用引用约束来标识某个属性充当关系的外键。Entity Framework uses a referential constraint to identify that a property acts as the foreign key for a relationship.

如果在创建关系时选中了 " 将外键属性添加到 < 实体类型名称 > 实体 " 复选框,则会为你添加此引用约束。If you checked the Add foreign key properties to the <entity type name> Entity checkbox when creating a relationship, this referential constraint was added for you.

使用 EF 设计器添加或编辑引用约束时,EF 设计器会ReferentialConstraint   在 .EDMX 文件的 CSDL 内容中添加或修改 ReferentialConstraint 元素。When you use the EF Designer to add or edit a referential constraint, the EF Designer adds or modifies a ReferentialConstraint element in the CSDL content of the .edmx file.

  • 双击要编辑的关联。Double-click the association that you want to edit. 此时将显示 " 引用约束"   对话框。The Referential Constraint dialog box appears.

  • 从 " 主体"   下拉列表中,选择引用约束中的主体实体。From the Principal drop-down list, select the principal entity in the referential constraint. 实体的键属性将添加到对话框中的 主体键   列表。The entity's key properties are added to the Principal Key list in the dialog box.

  • 从 " 依赖项"   下拉列表中,选择引用约束中的依赖实体。From the Dependent drop-down list, select the dependent entity in the referential constraint.

  • 对于具有依赖键的每个主体键,从 " 依赖键" 列中的下拉列表中选择相应的依赖项   。For each principal key that has a dependent key, select a corresponding dependent key from the drop-down lists in the Dependent Key column.

    Ref 约束

  • 单击 "确定"Click OK.

创建和编辑关联映射Create and Edit Association Mappings

您可以在 EF 设计器的 "映射详细信息" 窗口中指定关联映射到数据库的方式   。You can specify how an association maps to the database in the Mapping Details window of the EF Designer.

备注

只能映射未指定引用约束的关联的详细信息。You can only map details for the associations that do not have a referential constraint specified. 如果指定了引用约束,则会在实体中包含一个外键属性,您可以使用该实体的映射详细信息来控制外键映射到的列。If a referential constraint is specified then a foreign key property is included in the entity and you can use the Mapping Details for the entity to control which column the foreign key maps to.

创建关联映射Create an association mapping

  • 右键单击设计图面中的关联,然后选择 " 表映射"。Right-click an association in the design surface and select Table Mapping. 这会在 " 映射详细信息" 窗口中显示关联映射   。This displays the association mapping in the Mapping Details window.

  • 单击 " 添加表或视图"。Click Add a Table or View. 此时将显示一个下拉列表,其中包含存储模型中的所有表。A drop-down list appears that includes all the tables in the storage model.

  • 选择关联要映射到的表。Select the table to which the association will map. 映射详细信息"   窗口显示关联的两端以及每一端的实体类型的键属性。The Mapping Details window displays both ends of the association and the key properties for the entity type at each End.

  • 对于每个键属性,请单击 " "   字段,然后选择属性将映射到的列。For each key property, click the Column field, and select the column to which the property will map.

    映射详细信息4

编辑关联映射Edit an association mapping

  • 右键单击设计图面中的关联,然后选择 " 表映射"。Right-click an association in the design surface and select Table Mapping. 这会在 " 映射详细信息" 窗口中显示关联映射   。This displays the association mapping in the Mapping Details window.
  • 单击 " 映射到 < 表 > 名称"。Click Maps to <Table Name>. 此时将显示一个下拉列表,其中包含存储模型中的所有表。A drop-down list appears that includes all the tables in the storage model.
  • 选择关联要映射到的表。Select the table to which the association will map. 映射详细信息"   窗口显示关联的两端以及每一端的实体类型的键属性。The Mapping Details window displays both ends of the association and the key properties for the entity type at each End.
  • 对于每个键属性,请单击 " "   字段,然后选择属性将映射到的列。For each key property, click the Column field, and select the column to which the property will map.

编辑和删除导航属性Edit and Delete Navigation Properties

导航属性是快捷方式属性,用于在模型中关联的两端查找实体。Navigation properties are shortcut properties that are used to locate the entities at the ends of an association in a model. 在创建两个实体类型之间的关联时可以创建导航属性。Navigation properties can be created when you create an association between two entity types.

编辑导航属性To edit navigation properties

  • 选择 EF 设计器图面上的导航属性。Select a navigation property on the EF Designer surface. 有关导航属性的信息将显示在 Visual Studio 的 " 属性"   窗口中。Information about the navigation property is displayed in the Visual Studio Properties window.
  • 在 " 属性" 窗口中更改属性设置   。Change the property settings in the Properties window.

删除导航属性To delete navigation properties

  • 如果概念模型中的实体类型上没有公开外键,则删除导航属性可能导致仅在一个方向遍历相应的关联,或者根本不遍历关联。If foreign keys are not exposed on entity types in the conceptual model, deleting a navigation property may make the corresponding association traversable in only one direction or not traversable at all.
  • 右键单击 EF 设计器图面上的导航属性,然后选择 " 删除"。Right-click a navigation property on the EF Designer surface and select Delete.