Database Reference
In-Depth Information
With foreign key associations, you can change the association by changing object references or by directly
changing the foreign key property value. Foreign key associations are not used for many-to-many relationships.
Keep in mind that Foreign Key associations are simpler, easier, the default approach and recommended by
the entity Framework team. Unless you have a concrete business reason to use an independent association, always
consider using a Foreign Key association.
Note
Table 5-1 illustrates the main differences between foreign key associations and independent associations.
Table 5-1. The Differences Between Foreign Key Associations and Independent Associations
Foreign Key Association
Independent Association
Can be set using foreign key and navigation properties
Can only be set using a navigation property
Is mapped as a property and does not require a separate
mapping
Is tracked independently from the entity, which means
that changing the association does not change the state
of the entity
Data binding scenarios are easier because they can bind
to a property value.
Data binding is complicated because you have to create
a property manually, which reads the foreign key value
from the entity key, or traverse the navigation property
to load the related key.
Finding the old value for a foreign key is easier because it
is a property of an entity.
Accessing an old relationship is complicated because
relationships are tracked separately.
To delete an entity that uses a foreign key association, you
only need the entity key.
To delete an entity that uses an independent
association, you need the entity key and the original
values for all reference keys.
N-Tier scenarios are easier because you don't have to send
the related end's entity key along with the entity.
The client must send the related end's entity key value
along with the entity when the entity is attached. Entity
Framework will create a stub entry, and the update
statement includes the related end's entity key.
Three representations of the same association are kept
in sync: the foreign key, the reference, and the collection
navigation property on the other side. Entity Framework
handles this with the default code generation.
Two representations are kept in sync: the reference and
the navigation property
When you load a related entity, Entity Framework uses the
foreign key value currently assigned on the entity, not the
foreign key value in the database.
When you load a related entity, the foreign key value
is read from the database and, based on this value, the
related entity is loaded.
 
 
Search WWH ::




Custom Search