Entity Framework Code First Approach Mappings

How to create relationships between entities in the Entity Framework Code First Approach?

The Entity Framework Code First approach allows us to create a model as a plain class and then the database gets created from the domain model or entity class. In the Code First approach, the database gets created from the classes

In this post we will learn how we can create entities and a relationship between entities in the Entity Framework Code First approach

In the EF Code First approach, there are two options to create the relationship between entities, through:

1.      Data annotations
2.      Fluent API

In this post we will use data annotations to create the relationship between entities.

namespace CodeFirstDemoApp
{

   public class StudentInfo
    {

       public int SNo { get; set; }

       public string SName { get; set; }

       public int SAge { get; set; }

    }

}

Entity Framework will use the StudentInfo class to create the StudentInfo table.

Once the domain entity class is created, next we need to create a Context class which will inherit the DataContext class

using System.Data.Entity;

namespace CodeFirstDemoApp
{

    public class Context : DbContext

    {

        public Context() : base()

        {

        }

        public DbSet<StudentInfo> StudentInfo { get; set; }

    }

}

Creating the default constructor. Since we are not passing any parameter in the constructor, so the EF will create a database with the name as Namespace.Class name.

So in this case, the created database name will be CodeFirstDemo.Context.

 Since we are not passing any connection string information to the constructor thats why its creating database  in the default database server of SQL Server Express

if you want to create database in desired server we need to pass connection string.

To create the table in the database, create a public property of the generic DbSet type with domain entity passed in it.

However we can pass the desired name of the database StudentInfoDB in this case in the constructor as shown in the listing below:


public Context(): base("StudentInfoDB")

        {           

        }

We have created a connection string to create a database in local db server. Next we need to pass the connection string name in the constructor of Context class as shown in the listing below:

  public Context() : base("name=StudentInfoDBconnectionstring")

        {

           
        }

One to One Relationship Mapping:

 Let us say we have two entities and the following rules:

 Let us say we have two entities and the following rules:

1.      There are two entities named StudentInfo and StudentAccountInfo

2.      StudentInfo is a primary entity

3.     StudentAccountInfo is a dependent entity on StudentInfo

4.     Primary key of StudentAccountInfo will be foreign key of StudentInfo 

 public class StudentInfo

    {

       public int SNo { get; set; }

       public string SName { get; set; }

       public int SAge { get; set; }

      public virtual StudentAccountInfo StudentAccounts { get; set; }

    }

 public class StudentAccountInfo

    {

        public int SNo { get; set; }

        public string SName { get; set; }

        public int SAmount { get; set; }

         [Required]
        public virtual StudentInfo Students { get; set; }

    }


Again you might have noticed that in the StudenAccountInfo entity we have a virtual property of the type StudentInfo. Since StudentInfo is a primary entity, the virtual StudentInfo property in the StudentAccount Infoentity is annotated Required. Further, a Context class can be created as shown in the listing below:
using System.Data.Entity;
namespace CodeFirstDemoApp
{

    public class Context : DbContext

    {

        public Context()

            : base("name=StudentInfoDBconnectionstring")

        {

        }

        public DbSet<StudentInfo> Students { get; set; }

        public DbSet<StudentAccountInfo> StudentAccounts{ get; set; }

    }

}

One to Many Relationship 

We may have a requirement to create one too many relationship between two entities. Let us say we have two entities

1.There are two entities StudentInfo and StudentAddressInfo

2.StudentInfo is a primary entity

3.StudentAddressInfo is a dependent entity on StudentInfo

4.One StudentInfo can enroll in multiple StudentAddressInfo

One StudentInfo can have many StudentAddressInfo.

One of the column of StudentAddressInfo will have foreign key as primary key of StudentInfo.

public class StudentInfo

    {
      public StudentInfo()

       {

           StudentAddressInfoes = new HashSet<StudentAddressInfo>();

       }

       public int SNo { get; set; }

       public string SName { get; set; }

        public int SAge { get; set; }

       public ICollection<StudentAddressInfo> StudentAddress { get; set; }

   }


public class StudentAddressInfo

    {

        public int SNo { get; set; }

        public string  SAddress { get; set;}

        public int StudentId { get; set; }

        public virtual StudentInfo Students { get; set; }

    }

public class Context : DbContext

    {

        public Context()  : base("name=StudentInfoDBconnectionstring")

        {

        }

        public DbSet<StudentInfo> Students { get; set; }

        public DbSet<StudentAddressInfo> StudentAddress { get; set; }

    }


Many to Many Relationship 

 Let’s say we have a StudentInfo entity and a SubjectInfo entity. One Student can be enrolled in many Subjects and One Subject can have many Students

public class StudentInfo

    {
      public StudentInfo()

       {

           Subjects = new HashSet<Subject>();

       }

       public int Id { get; set; }
       public string Name { get; set; }
       public int Age { get; set; }
       public ICollection<SubjectInfo> Subjects { get; set; }
    }


 public class Subject

    {

        public Subject()

        {

            StudentInfos = new HashSet<StudentInfo>();
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public virtual ICollection<StudentInfo> Students { get; set; }

    }


public class Context : DbContext

    {

        public Context()

            : base("name=StudentInfoDBconnectionstring")
        {

           
        }

        public DbSet<StudentInfo> Students { get; set; }

        public DbSet<SubjectInfo> Subjects { get; set; }

    }


Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment