Associations
Associations are relationships beteween two entities, termed “navigation properties” in Entity Framework documentation.
The designer supports creation of both unidirectional and bidirectional associations. In unidirectional associations, the source entity contains a property of the destination type, but not the other way around. In bidirectional associations, each class contains a property pointing to the other entity.
As an example, consider the following:
This tells us that Entity1
has a collection of Entity2
objects named TheEntity2s
. The type of collection will typically be set as the default value
set in the design surface, but can be overridden (see the Properties tables, below).
In short, this means that Entity1
would contain
public virtual ICollection<Entity2> TheEntity2s { get; set; }
but Entity2
doesn’t have a matching property.
In contrast, a bidirectional association like
would have, in the Entity2
class,
public virtual Entity1 TheEntity1 { get; set; } // Required
as well as the collection of Entity2
objects in the Entity1
class. (Phew! Extensive overuse of the word Entity !)
Cardinalities
Associations have cardinalities (the number of elements in a set) at each end. Supported cardinalities are:
- Zero or One (0..1)
- One (1)
- Zero or More (*)
While cardinalities could technically include One or More (1..*)
, Entity Framework currently doesn’t directly support that. That doesn’t mean
that it can’t be done, just that it has to be done with custom code (and, in fact, is on the to-do list of things
to support with custom code generation in the designer).
Regardless of whether the association is unidirectional or bidirectional, it’s important to correctly specify the cardinality at each end. It has an effect on the kind of database structure Entity Framework creates, as well as being critical to properly modelling the problem space.
It can’t be emphasized enough - getting an association’s cardinality correct is extremely important. Lots of application code will have to change if you wind up changing the cardinalities after the entities are in use.
Unidirectional Association Properties
Unidirectional associations have the following properties:
Property | Description |
---|---|
Code Generation | |
Collection Class | String. The concrete class used to implement Zero or More cardinality ends. Defaults to class set at the designer level, but can be overridden here. Must implement ICollection<T>. |
Persistent | Boolean. If false, the association will not be persisted in the database, requiring custom code to implement. |
Documentation | |
Comment Summary | String. XML comment <Summary> section |
End1 | |
End1 Multiplicity | String. Cardinality of this end |
End1 On Delete | String. Only appears if End1's role is Principal. Describes how to handle dependent objects (the ones on the other end of the association) if the principal object (the one on on this end) is deleted. Options are
|
End1 Role | String. Whether this end is the Principal or Dependent end of the association. For more information, see Entity Framework Relationships and Navigation Properties |
End2 | |
End1 Custom Attributes | String. Attributes generated in the code for this element - anything here will be generated verbatim into the code in the class definition. |
End1 Display Text | String. Will cause the generation of a [Display(Name="<text>")] attribute tag in the code. |
End1 Navigation Property | String. Name of the property that will be generated in the class on the *other* side of the association. |
End2 Comment Detail | String. XML comment <Remarks> section for this end |
End2 Comment Summary | String. XML comment <Summary> section for this end |
End2 Multiplicity | String. Cardinality of this end |
End2 On Delete | String. Only appears if End2's role is Principal. Describes how to handle dependent objects (the ones on the other end of the association) if the principal object (the one on on this end) is deleted. Options are
|
End2 Role | String. Whether this end is the Principal or Dependent end of the association. For more information, see Entity Framework Relationships and Navigation Properties |
Bidirectional Association Properties
Bidirectional associations have the following properties:
Property | Description |
---|---|
Code Generation | |
Collection Class | String. The concrete class used to implement Zero or More cardinality ends. Defaults to class set at the designer level, but can be overridden here. Must implement ICollection<T>. |
Persistent | Boolean. If false, the association will not be persisted in the database, requiring custom code to implement. |
Documentation | |
Comment Summary | String. XML comment <Summary> section |
End1 | |
End1 Comment Detail | String. XML comment <Remarks> section for this end |
End1 Comment Summary | String. XML comment <Summary> section for this end |
End1 Multiplicity | String. Cardinality of this end |
End1 On Delete | String. Only appears if End1's role is Principal. Describes how to handle dependent objects (the ones on the other end of the association) if the principal object (the one on on this end) is deleted. Options are
|
End1 Role | String. Whether this end is the Principal or Dependent end of the association. For more information, see Entity Framework Relationships and Navigation Properties |
End2 Custom Attributes | String. Attributes generated in the code for this element - anything here will be generated verbatim into the code in the class definition. |
End2 Display Text | String. Will cause the generation of a [Display(Name="<text>")] attribute tag in the code. |
End2 Navigation Property | String. Name of the property that will be generated in the class on the *other* side of the association. |
End2 | |
End1 Custom Attributes | String. Attributes generated in the code for this element - anything here will be generated verbatim into the code in the class definition. |
End1 Display Text | String. Will cause the generation of a [Display(Name="<text>")] attribute tag in the code. |
End1 Navigation Property | String. Name of the property that will be generated in the class on the *other* side of the association. |
End2 Comment Detail | String. XML comment <Remarks> section for this end |
End2 Comment Summary | String. XML comment <Summary> section for this end |
End2 Multiplicity | String. Cardinality of this end |
End2 On Delete | String. Only appears if End2's role is Principal. Describes how to handle dependent objects (the ones on the other end of the association) if the principal object (the one on on this end) is deleted. Options are
|
End2 Role | String. Whether this end is the Principal or Dependent end of the association. For more information, see Entity Framework Relationships and Navigation Properties |