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.
Now, we will create classes with properties.
Artist.cs
- public class Artist
- {
- public int ArtistId { get; set; }
- [Required()]
- [StringLength(100,MinimumLength =2)]
- public string Name { get; set; }
- public string Bio { get; set; }
- public virtual ArtistDetails ArtistDetails { get; set; }
- }
ArtistDetails.cs
- public class ArtistDetails
- {
- public int ArtistId { get; set; }
- public string Bio { get; set; }
- public virtual Artist Artist { get; set; }
- }
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.
After that, add a DBContext class in the Models folder.
- public class ArtistDbContext
- {
- public class EmpDataContext : DbContext
- {
- public EmpDataContext()
- : base("name=MySqlConnection")
- {
-
- }
- public DbSet<Artist> artists { get; set; }
- public DbSet<ArtistDetails> artisDetails { get; set; }
- }
- }
Then, we will set the connection string in web.config.
- <connectionStrings>
- <add name="MySqlConnection" connectionString="Data Source=NewPC;database=MyDemoDB;User Id=sa;Password=123;"
- providerName="System.Data.SqlClient" />
- </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 link. After that, go to SQL Server and check the table and relation.
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 .
- public class Reviewer
- {
- [Key]
- public int RevierrId { get; set; }
- public string Name { get; set; }
- public virtual List<ArtistDetails> ArtistDetails { get; set; }
- }
Now, we will update the database using console command.
After that, we will check in SQL.
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.