Free Web Development Online Tutorials, Learn to Code
entity framework core crud operations c#, asp.net core crud operation using entity framework, EF Core CRUD operations

Entity Framework Core CRUD Operations Examples

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

You can use this entire examples to work with Asp.Net Core or any other type of application written using .Net Core C#.

In this example 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 t, 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.

Assumption: we assumed you already familiar with DbContext class and how to set up connection string in .Net Core

Comment
Name
Email
Website
Subscribe
Entity Framework C# Examples | Join .Net C# Course