Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Update data using Entity Framework Core

Here we learn how to update data using entity framework core.
How Update works in EF Core
In entity framework core EntityState is changed automatically, EF Core API can keep track of all the entities retrieved using the context, so when you make any changes in any context object, EntityState become Modified, and finally when we call the SaveChanges() method, all new changes made to context gets saved to database.

Updating single table using EF Core

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;
}

Updating multiple tables using EF Core

Updating collection object using EF Core


 
Update example in EF Core

Group Training