The Relationships in entity framework can be done using the following methods Hence our entity classes will be as shown belowĬonfigure Many to Many relationships in Entity Framework Configuring the Relationship in Entity Framework In a One to one relationship PrimaryKey of one table (employeeID of employee table) is both Primary key and Foreign key in the second table (EmployeeAddress). This will create a one to one relationship between employee and EmployeeAddress table. Let us assume that employee can have only one address. Because you can combine them into one table easily without breaking any rule.įor our example, consider the domain class Employee and EmployeeAddress. One to one relationship usually does not happen in database design. The Relationship between husband and wife is an example of one to one relationship. There are three specific types of relationships can exists between tables
An Employee can be part of many projects and projects can have many employees working under it. For Example, Employee can belong to one department, but the department can have many employees. The relationships can come in various types. The Employees Collection in the Department Class
They allow us to navigate from one end of the relationship to the other end. Navigation properties describe the relationship between two entity types.
The DepartmentID in the Department Entity is the Principal Key Navigation PropertiesĪ Navigation Property is a property that defined on the principal and/or dependent entity that contains a reference to the related entity The Principal Key can be either primary key or an alternate key. The Principal Key is the property that uniquely identifies the Principal Entity and participates in the relationship. In the above example, DepartmentID is the Foreign Key in the Dependent Entity, which links Employee to Department. It is stored in the Dependent Entity and points to the Parent Entity. The Foreign Key is the glue between Principal Entity and its Dependent Entity. The Dependent Entity contains the foreign key property. The Employee Entity is the Dependent Entity here as it needs Department Entity. The Dependent Entity (or child) dependent on Principal Entity. It will contain the primary/alternate key properties, which will be inserted into the Depdendent Entity Dependent Entity The Principal Entity can exist on its own. The Department Model is the Principal entity (or parent) as Employee entity needs it. The Best EF Core Books, which helps you to get started with EF Core Principal Entity The entities participating in the relation can be either Principal entity or Dependent Entity