Free Web Development Online Tutorials, Learn to Code
CRUD operations in Entity Framework with C#, Entity Framework Create, Read, Update, Delete Example

Entity framework CRUD Operations in C#

In this tutorial we will learn all CRUD Operations in Entity Framework, How to create or add, select or read, update or edit and delete data using entity framework in C#

Entity Framework Create, Read, Update, Delete

In each of following examples, you see use of db context class, which is Entities in our example.

Before you start with this exercise, you should be familiar with DBContext object (ex: Entities context = new Entities()).

Create or Add data example

We can simply add the entity to database and also can return the added object to calling function, so they know object has been added successfully.

public tbStudent AddStudent(tbStudent student)            
{
    tbStudent _student = null;

    using (Entities context = new Entities())
    {
        _student = context.tbStudents.Add(student);
        context.SaveChanges();
    }

    return _student;
}

Read or Select data example

This method will return a collection object. List of student object, if there is no record in database, this will null object.

public List<tbStudent> getAllStudents()
{
    List<tbStudent> _students = null;

    using (Entities context = new Entities())
    {
        _students = context.tbStudents
        .ToList<tbStudent>();
    }

    return _students;
}

Read or Select data with Where clause example

This method will return a student object based on matching id, if the matching id is not found in database, the method will return null object.

Notice, we have use FirstOrDefault method, so if the matching record not found, it will return null, we could have used First method also, but in that case if matching record not found it would throw exception, that has to be taken care in code.

public tbStudent getStudent(int id)
{
tbStudent _student = null;
using (Entities context = new Entities())
{
_student = context.tbStudents
.Where(s => s.studentId == id)
        .FirstOrDefault<tbStudent>();
    }

    return _student;
}

Update or Edit data in Entity Framework

Before we update any entity, we need to fetch the entity from database matching with provided id, so in following example we first fetch the object from, then update each property value one by one, finally save the entity using context object SaveChanges method.

You must set the object state to modified State = EntityState.Modified;, before calling the SaveChanges method.

public bool updateStudent(tbStudent student) {
tbStudent _student = null;
bool _isDone = false;
using (Entities context = new Entities())
{
_student = context.tbStudents
.Where(s => s.studentId == student.studentId)
        .FirstOrDefault<tbStudent>();
            
        if (_student != null)
        {
            // updating new values
            _student.FullName = student.FullName;
            _student.email = student.email;
            
            context.Entry<tbStudent>(_student).State = EntityState.Modified;
            context.SaveChanges();
            _isDone = true;
        }
    }
            
    return _isDone;
}

Delete Example Entity Framework

Delete Data with Where clause using Entity Framework Example

public bool deleteStudent(int id) {
tbStudent _student = null;
bool _isDeleted = false;
using (Entities context = new Entities())
{
_student = context.tbStudents
.Where(s => s.studentId == id)
        .FirstOrDefault<tbStudent>();
            
        if (_student != null)
        {
        context.Entry<tbStudent>(_student).State = EntityState.Deleted;
        context.SaveChanges();
        _isDeleted = true;
        }
    }
return _isDeleted;
}

Above CRUD operation code is tested working properly, can be implemented in your project, only change the object name

If you are using entity framework core, then you should look at EF Core CRUD operations examples.

Comment
Name
Email
Website
Subscribe
 
learn entity framework
Entity Framework CRUD Operation

Entity Framework Interview Questions

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