Entity Relationships:

You can have three types of relationships in EDM as in database. 1) One to One 2) One to Many 3) Many to Many.

Let's examine the database table design before going into the relationships in EDM. The following figure is a database diagram of SchoolDB used in this tutorial.

As you can see in the figure, student and StudentAddress have a One-to-One relationship, where each student has either zero or one address.

Standard and Teacher have a One-to-Many relationship, where one standard can have more than one Teacher but one Teacher can't have many Standards.

Student and Course have a Many-to-Many relationship, by using the StudentCourse table. StudentCourse consists of a primary key of both the tables and thus it making it a Many-to-Many relationship.

When you create an ADO.NET Entity Data Model from the SchoolDB database, using the EF Designer from database option, it will create the following entities and relationships in EDM:

As you can see in the above figure, Student and StudentAddress have a One-to-One relationship (zero or one). StudentAddress entity has StudentId property as PK which makes it a One-to-One relationship.

Standard and Teacher have a One-to-Many relationship marked by multiplicity where 1 is for One and * is for many. The Standard entity has the navigation property “Teachers” which indicates that one Standard can have a collection of Teachers and the Teacher entity has a Standard navigation property which indicates that Teacher is associated with one Standard. This makes it One-to-Many relationship.

Student and Course have Many-to-Many relationships marked by * multiplicity, but it doesn't display entityset for the middle tableStudentCourse, where primary key of both tables will be stored. This is because the EDM represents many-to-many relationships, by not having entityset for the joining table in CSDL, instead it manages this through mapping. It can do this only when the joining table doesn't have any columns other than PKs of both the tables. If the join tables had additional columns, such as DateCreated, then the EDM would have created entities for them and you would have to manage Many-to-Many relationship entities manually.

Many-to-Many relationship in EDM:

Open EDM in XML view. You can see that SSDL has StudentCourse entityset, but CSDL doesn't have StudentCourse entityset instead, it's being mapped in the navigation property of the Student and Course entity. In MSL (C-S Mapping), it has mapping between Student and Course put into the StudentCourse table in <AssociationSetMapping/>

Thus Many-to-Many relationship is being managed by C-S mapping in EDM. So when you add a Student in a Course or a Course in a Student entity and when you save it, it will then insert PK of the added student and course in StudentCourse table. So this mapping not only enables a convenient association directly between the two entities, but also manages querying, inserts, and updates across this joint.

Note: EDM does this only when joining table has PK columns for both tables. If you have some other columns in the joining table, then EDM will treat them as normal entitys and you have to use 'Join' in your query to fetch the data.

Entity Graph:

When an entity has a relationship with other entities, then the full object hierarchy is called an 'entity graph'. For example the following is a Student entity graph, that includes its full hierarchy object Standard, StudentAddress & Course.