Configuring relationships: Overview

Description Article
Summary

An entity relationship is implemented in the POCO entity classes by defining a navigation property that references the other entity class type.

When the multiplicity is many (e.g. 0..*, 1..*) the relationship is implemented via an ICollection<TENTITY> of the related type.

Consequently, objects that refer to each other using property references or collections of references can be navigated using “dot” notation.

Description

A relationship defined in the code should reflect:

1. An existent Primary Key/Foreign Key defined (existent database)/to define (new database) in the relational database;
2. A configuration using a DataAnnotation attribute or via the Fluent API

In order to identify a relationship with DataAnnotation attributes the focus of the analysis is its navigation properties.

In order to identify a relationship with the Fluent API the Model Builder is used: The pattern followed uses a two part fluent expression to configure navigation properties within the model:

Entity.Has[Multiplicity](Property).With[Multiplicity](Property)

where [Multiplicity] can be:

  • Optional (a property that can have a single instance or be null),
  • Required (single instance),
  • Many (a property with a collection of a single type)

and

  1. The first part of the expression which defines the navigation property on the current entity; and
  2. The second part of the expression which defines the reverse navigation property.

How To Do It

Configuring One-To-Zero or One Relationships
Configuring One-To-Many Relationships
Configuring Many-To-Many Relationships  

 

 

References

[1] Refer to the following articles for information about configuring One-To-Zero or One using Fluent API:

[2] Refer to the following articles for information about configuring One-To-Zero or One using Fluent API and DataAnnotation:

[3] Refer to the following articles for information about configuring One-To-Many using Fluent API and DataAnnotation:

[5] Refer to the following articles for information about configuring relationships with the Fluent API:

[56 Refer to the following articles for questions and answers about configuring relationships (usefull samples):