Code first procedure in Entity Framework Core (Part 1)

Here I am going to show in step by step how to make database using Entity framework core (Code first) :

Step 1 (Create project):
Create a new console application. Give the project a name. I give my project name “FECore001”.

Step 2 (Add library):
Now add new class library in the project like below and remove class1.cs :

now my project looks like below :

Step 3 (Create classes):

Now create three class named “School”, “Teacher” and “Student” in “FECore001.MyClassLibrary” library.

Lets consider School and Teacher have one to many relation,
and Teacher and Student have one to many relation.

School Class :

public class School
{
public School()
{
Teachers = new List<Teacher>();
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime StartDate { get; set; }
public List<Teacher> Teachers { get; set; }
}

Teacher Class :

public class Teacher
{
public Teacher()
{
Students = new List<Student>();
}
public int Id { get; set; }
public string Name { get; set; }
public School School { get; set; }
public int SchoolId { get; set; }
public List<Student> Students { get; set; }
}

Student Class :

public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Teacher Teacher { get; set; }
public int TeacherId { get; set; }
}

Step 4 (Add EntityFrameworkCore NuGet Package): 

Now I am going to add EntityFarmeworkCore in this project using NuGet Package:
Right click on “FECore001.MyClassLibrary” and select “Manage NuGet Packages”.

 

Select browse.
Then Search item “Microsoft.EntityFrameworkcore.sqlserver”.
Then click install.

Same as install “Microsoft.EntityFrameworkcore.tools”.

After installing this package we can see all the packages are install under reference that we need for entity framework core.

Step 5 (Create MyContext class) :

       using Microsoft.EntityFrameworkCore;

namespace FECore001.MyClassLibrary
{
public class MyContext : DbContext
{
public DbSet<School> Schools { get; set; }
public DbSet<Teacher> Teachers { get; set; }
public DbSet<Student> Students { get; set; }
}
}

Step 6 (Add connection string in MyContext class) :

Add below override method in MyContextClass.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(“Data Source=.;Initial Catalog=MySchool;Integrated                                                                                Security=True”);
}

Here my database name will be MySchool after migrate my code.

Step 7 (Migration) :

Now I am going to add a migration that make relation between entities.
Select tools => NuGet Package Manager => package manager console. Like below :

Now set “FECore001.MyClassLibrary” as start up project and select project name and write “add-migration Initial” like below image :

Now click enter.
Now we can see a folder named “Migrations” and two classes under it.

Now open “20170407164404_Initial” where we can see migration detail and in “MyContextModelSnapshot” we can see mapping.

Step 8 (Create Database) : 
Now we are going to create database. in comman shell write “update-database -verbose” add press enter.

Now open sql server and see that “MySchool” database is created.

 

Done. Thanks for reading. 🙂

Leave a Reply