Entity Framework core Model
Relationships
Nangarhar University Computer Science Faculty –(NUCSF) 1401/12/03
oultines
1. What is relationship
2. Types of Relationship
1. One-to-one relationship
2. One-to-many relationship
3. Many-to-many relationship
3. Tow types of implementation
1. Data annotation
2. Fluent API
Relationship
✓ Relationship in [Link] core’s model classes is an association
between them.
✓ The way that modal classes interact with each other such that :
✓An instance of one entity is associated with one or more instances of
the other entity classes.
one-to-one relationship
• An instance of one Entity is associated with just one instance of the
another Entity.
• e.g:
• We have to model classes one is employee and another is
Department.
• In our business environment every department has just on head of
the department the HOD is among the employee.
• It means the employee could head of maximum of one department
and a department must have only one head of the department.
one-to-one relationship
The department class with navigation property.
• public class Department
• {
• public int Id { get; set; }
• public string Name { get; set; }
• public Employee Employee { get; set; }
• }
one-to-one relationship
The Employee class with navigation property.
• public class Employee
• {
• public int Id { get; set; }
• public string Name { get; set; }
• public string Address { get; set; }
• public Department Department { get; set; }
• }
One-to-Many Relationship
• In one-to-many Relationship one instance of an Entity is associated
with one or more instance of the another entity.
• e.g:
• we have tow modal classes customer and country.
• A country has more than one customer and a customer is belongs to
only one country.
• We can implement the above relationship tow ways one is data
annotation and another way is fluent api.
• Let’s first implement this with the simple way this is data annotation.
One-to-Many Relationship
with data annotation
• Tow modal classes are:
cont…
• Note:
• If the primary key filed of a model class Id or (modelnameId) then the
migration by convention create the primary key for table otherwise it
can not map.
• E.g: there is a model class named customer if the key property a
customer class is (Id) or (CustomerId) the migration has now problem
• if we went to name primary key for customer other than (Id) or
(customerId).
• In this case [key] data annotation must be used above the property.
• [Key]
• public int customer_Id { get; set; }
cont…
• To implement one to many relationship with data annotion Just do
the following change to customer and country classes.
cont…
• If the primary key property dose not follow the convention then do
the following in customer class for foreign key with the name of your
choice.
Many-to-Many Relationship
• For implementing many-to-many binary relationship we need three
model classes.
• Tow ot them are the associated classes and the third one is the joined
or intermediate classes for making many to many association.
• E.g:
• We have tow tables Course and Student.
• In this scenario a student take one or more than one courses and also
a course has one or more than one students.
Many-to-Many Relationship
Student Class
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
Many-to-Many Relationship
Course Class
public class Course
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Author { get; set; }
public string Gender { get; set; }
}
Many-to-Many Relationship
• After we implement the many to many relationship the following
changes must be made in both classes.
public class Course
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Author { get; set; }
public string Gender { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set;
}
}
Many-to-Many Relationship
• After we implement the many to many relationship the following
changes must be made in both classes.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
}
Many-to-Many Relationship
• Now the time is for adding the bridge entity class.
• The intermediate class name is StudentCourse.
• With following properties.
public class StudentCourse
{
[ForeignKey("Student")]
public int StudentId { get; set; }
[ForeignKey("Course")]
public int CourseId { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
}
Many-to-Many Relationship
• As the StudentCourse class primary key may be the combination of
the tow fileds which are studentId and CourseId.
• The data annotation dose not have any method for composite key so
for this purpose we should use the fluent api to configure composite
key for this model class.
• protected override void OnModelCreating(ModelBuilder modelBuilder)
• {
• [Link]<StudentCourse>()
• .HasKey(sc=> new {[Link], [Link] });
• }
Relationship Via Fluent API
➢Fluent API is an advanced way of doing model configuration.
➢Fluent API can do all things which can be done by data annotation.
➢Fluent API can do some advanced configuration which is not possible by
data annotation to be done.
➢The Fluent API Model configuration is done by overriding
OnModelCreating() method of the DbContext class.
➢Code first prefer fluent API over data annotation.
// Method in DBContext Classs.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Fluent API configuration goes here.
}
Fluent API one-to-one relationship
•
• [Link]<Department>()
• .HasOne(d => [Link])
• .WithOne(e => [Link])
• .HasForeignKey<Department>(x =>
[Link]);
Fluent API one-to-many relationship
•
• [Link]<Customer>()
• .HasOne(f => [Link])
• .WithMany(f => [Link])
• .HasForeignKey(f => [Link]);
Fluent API many-to-many relationship
• [Link]<StudentCourse>()
• .HasKey(k => new { [Link], [Link] });
• [Link]<StudentCourse>()
• .HasOne(s => [Link])
• .WithMany(sc => [Link])
• .HasForeignKey(sf => [Link]);
• [Link]<StudentCourse>()
• .HasOne(c => [Link])
• .WithMany(sc => [Link])
• .HasForeignKey(cf => [Link]);
Fluent API Organization
• public class CustomerConfiguration : IEntityTypeConfiguration<Customer>
• {
• public void Configure(EntityTypeBuilder<Customer> builder)
• {
• [Link](c => [Link]);
• }
• }
• [Link](new CustomerConfiguration());
Its your turn to ask about today’s lecture