Note: This page provides information about setting up relationships in your model using the EF Designer. 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. This topic shows how to map associations with the Entity Framework Designer (EF Designer). The following image shows the main windows that are used when working with the EF Designer.
Note: 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.
The topic shows how to perform the following tasks.
When you design your model using the EF Designer, an .edmx file represents your model. 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. 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). 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.
Note: In many-to-many (*:*) you cannot add foreign keys to the entities. In a *:* relationship, the association information is managed with an independent object.
For information about CSDL elements (ReferentialConstraint, Association, etc.) see the CSDL specification
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).
Note: This section assumes that you already added the entities you wish to create an association between to your model.
To delete an association do one of the following:
- OR -
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.
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.
You can specify how an association maps to the database in the Mapping Details window of the EF Designer.
Note: 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.
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.