Entity Framework In MVC - Part Eight

In the previous article, I have explained One- to- One relationship of code first approach in Entity Framework. In this article, I will explain one –to- one and one- to - Many relationships in code first approach in Entity Framework. Let’s see step by step.

In the previous article, I have explained the one-to-one relationship of the code first approach in Entity Framework. In this article, I will explain one-to-one and one-to-many relationships in the code first approach in Entity Framework. Let’s see step by step.

See the previous articles for basic details.

One-To-One Relationship

A one-to-one relationship happens when a primary key of one table becomes the Foreign Key (FK) in another table in a relational database.

In Entity Framework,

  • When we create a relationship, then the default is a one-to-many relationship.
  • When two classes point to one another, the Entity framework cannot determine which is the parent and which is the child.
  • One-to-One relationship must be explicitly created – Add Foreign Key to the child class.

Here, we will take two classes -  Artist and Artist Details.

Entity Framework in MVC

Now, we will create classes with properties.

Artist.cs

  1. public class Artist  
  2.   {  
  3.       public int ArtistId { get; set; }  
  4.       [Required()]  
  5.       [StringLength(100,MinimumLength =2)]  
  6.       public string Name { get; set; }  
  7.       public string Bio { get; set; }  
  8.       public virtual ArtistDetails ArtistDetails { get; set; }  
  9.   }  

ArtistDetails.cs

  1. public class ArtistDetails  
  2.   {  
  3.       public int ArtistId { get; set; }  
  4.       public string Bio { get; set; }  
  5.     public virtual Artist Artist { get; set; }  
  6.   }  

Now, let us define the relationship. Entity Framework does not know what should we do. So, now what we have to do is that ArtistDetails is child of Artist. For that, we have to add Foreign Key in ArtistDetails.

Entity Framework in MVC

After that, add a DBContext class in the Models folder.

  1. public class ArtistDbContext  
  2.   {  
  3.       public class EmpDataContext : DbContext  
  4.       {  
  5.           public EmpDataContext()  
  6.               : base("name=MySqlConnection")  
  7.           {  
  8.   
  9.           }  
  10.           public DbSet<Artist> artists { get; set; }  
  11.           public DbSet<ArtistDetails> artisDetails { get; set; }  
  12.       }  
  13.   }  

Then, we will set the connection string in web.config.

  1. <connectionStrings>  
  2.   <add name="MySqlConnection" connectionString="Data Source=NewPC;database=MyDemoDB;User Id=sa;Password=123;"  
  3.        providerName="System.Data.SqlClient" />  
  4. </connectionStrings>  

After that, go to Tools >> NuGet Package Manager >> Package Manager Console and Type on command.

Update-Database

If you want more details about Code first migration, then go to this linkAfter that, go to SQL Server and check the table and relation.

Entity Framework in MVC

Entity Framework in MVC

Now, we will see many-to-many relationships.

  • Relational database typically doesn’t support a many-to-many relationship. For this, we have to create a join table.
  • Entity Framework just knows how to add the properties to both sides and create the join table.

It's not possible to create a many-to-many relationship with a customized join table. In a many-to-many relationship, EF manages the join table internally. It's a table without an Entity class in our model. To work with such a join table with additional properties we  have to create two one-to-many relationships .

Now, we will take an example, I am going to add one more class in the Models folder .

  1. public class Reviewer  
  2.   {  
  3.       [Key]  
  4.       public int RevierrId { get; set; }  
  5.       public string Name { get; set; }  
  6.       public virtual List<ArtistDetails> ArtistDetails { get; set; }  
  7.   }  

Now, we will update the database using console command.

Entity Framework in MVC

After that, we will check in SQL.

Entity Framework in MVC

Entity Framework in MVC

The many-to-many relationship in the database is represented by a joining table which includes the foreign keys of both tables. Also, these foreign keys are composite primary keys.

Summary

Finally, in this article, we saw how to create one-to-one and many-to-many relationships using the code first approach. In our next article, we will see Data Annotation and Fluent API in the code first approach in Entity framework using MVC.