Entity Framework Core CRUD Operations Examples

Let's learn how to work with entity framework core, if you have worked with earlier version of entity framework, then you may find many similarities, but at the same time there are some new features or coding syntax introduced in this new .net core version of entity framework.

In this Entity Framework Core CRUD Operations c# tutorial I will explain you how to Create, Read, Update and Delete data using Entity Framework Core.

EF Core CRUD Operations

If you are completely new to entity framework core, then go through my earlier post to understand how entity framework core library installation done.

Here all the examples will demonstrate how to work with Asp.Net Core or any other type of application written using .Net Core C#.

Assumption: I am assuming you know how to create database connection in entity framework core , if you are not familiar, then please take a look at DbContext class and how to set up connection string in .Net Core.

Create or Add data using EF Core

Code for adding data to database using entity framework core.

To create any new record in database using entity framework, you need to fist create an instance of DbContent object , like context = new EFContext(). then add the new entity and finally call save changes method, like context.SaveChanges();.

public Student AddStudent(Student s)
{
    using (EFContext context = new EFContext())
    {
        context.tbStudent.Add(s);
        context.SaveChanges();
    }
    return s;
}

EF Core Read or Select data example

Here is the code for selcting or reading data from database using entity framework core If you want to read only one row, like one student information then use GetStudent(int sid) method, or if you want to students without any clause use GetAllStudents() method.

// example of reading a list of data
public List<Student> GetAllStudents()
{
List<Student> list = new List<Student>();
var context = new EFContext();
list = context.tbStudent
.ToList<Student>(); return list;
}
// example of reading a single row public Student GetStudent(int sid)
{
Student _s=null;
using (EFContext context = new EFContext())
{
_s = context.tbStudent
.Where(s => s.StuId == sid)
        .FirstOrDefault<Student>();
               
    }
    return _s;
}

EF Core Update data Example

The below example for updating data to database using entity framework core.

Before updating any row, you need to fetch that row from database using unique id, like in following example we have student.StuId, then change each property value with new values.

public Student UpdateStudent(Student student)
{
Student _s = null;
using (EFContext context = new EFContext())
{
_s = context.tbStudent
.Where(s => s.StuId == student.StuId)
                .FirstOrDefault<Student>();
        if (_s != null)
        {
            _s.Firstname = student.Firstname;
            _s.Lastname = student.Lastname;
            _s.Email = student.Email;
            _s.ContactNumber = student.ContactNumber;
            context.SaveChanges();
        }
    }
    return _s;
}

You can also look at another EF Core Update Example.

EF Core Delete data example

Code for deleting data from database using entity framework core

Before deleting any data in entity framework, you must get that data in your current context object, very similar to update process.

public bool DeleteStudent(int stuid)
{
bool _isDeleted = false;
Student _s = null;
var context = new EFContext();
_s = context.tbStudent
.Where(s=>s.StuId==stuid)
    .FirstOrDefault<Student>();
        if (_s != null)
        {
        context.tbStudent.Remove(_s);
        context.SaveChanges();
        _isDeleted = true;
        }
    return _isDeleted;
}

You can also look at another EF Core Delete example.

You should also read following posts

Entity Framework C# Examples | Join .Net C# Course