Use of HttpPatch in Web API (ASP.Net Core)

Use of AutoMapper in Web API for DTO (Data Transfer Object) or ViewModel (ASP.Net Core) with example

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. 🙂

CRUD Operation using Entity framework core – Code first procedure (Part -2)

Here I am going to show CRUD operation using entity framework core (Code first procedure).

Step 1 (Create project):

Create a project named “CRUD_EFCore”.

Step 2 (Install NuGet Packages) :

Right click on project name and select “Manage NuGet Packages”.
Browse “Microsoft.EntityFrameworkcore.sqlserver and install it.

Same as install package “Microsoft.Entityworkcore.tools”.

Step 3 (Create a Object) :

Lets create a class named “Student.cs”.

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

Step 4 (Create context class) :

Create a context class named “StudentContext.cs”.

using Microsoft.EntityFrameworkCore;

public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(“Data Source=.;Initial Catalog=DB_Student;Integrated Security=True”);
}
}

Step 5 (Add migration) :

Now I am going to add a migration.
Select tools => NuGet Package Manager => Package manager console.
Type add-migration firstMigration like below and press enter.

After doing successfully migration, we can see a Migrations folder and two classes automatically created under project.

Step 6 (Create database) :

Now we are going to create database. In command shell write “update-database -verbose” like below and press enter.

After done, open Sql server and see database part. We can see a database automatically created named “DB_Student”.

Basic set up is done. How CRUD part is start from below.

Step 7 (Create operation) :

Open Program.cs class.

private static StudentContext _context = new StudentContext();
private static void Main(string[] args)
{
CreateStudent();
}

private static void CreateStudent()
{
var student = new Student { Name = “Rahat”, Roll = “001” };
_context.Students.Add(student);
_context.SaveChanges();
}

Now run the project.
Output :



Step 8 (Retrieve operation) :

        private static StudentContext _context = new StudentContext();
private static void Main(string[] args)
{
//CreateStudent
RetrieveStudent();
}

private static void RetrieveStudent()
{
var students = _context.Students.ToList();
foreach (var student in students)
{
Console.WriteLine(student.Name + “,”);
Console.ReadLine();
}
}

Run the project.
Output :

Step 9 (Update operation) :

        private static StudentContext _context = new StudentContext();
private static void Main(string[] args)
{
//CreateStudent
//RetrieveStudent();
UpdateStudent();
}

private static void UpdateStudent()
{
var student = _context.Students.FirstOrDefault();
student.Name = “Sabbir”;
_context.SaveChanges();
}

Run the project.

Output :

Step 10 (Delete operation) :

        private static StudentContext _context = new StudentContext();
private static void Main(string[] args)
{
//CreateStudent
//RetrieveStudent();
//UpdateStudent();
DeleteStudent();
}

private static void DeleteStudent()
{
var student = _context.Students.FirstOrDefault(s => s.Name == “Sabbir”);
_context.Students.Remove(student);
_context.SaveChanges();
}
Run the project.
Output :

Done. 🙂