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

Leave a Reply