Menu ▼

This example shows you how to update multiple records in database table that satisfies given condition (where condition) in a single line by using LINQ query in entity framework.

Example :
using (DBEntities db = new DBEntities())
    db.tblTemp.Where(x => x.ProductID == parmProductID && x.IsActive == true).ToList().ForEach(x =>
        x.IsActive = false; x.UpdatedTimeStamp = DateTime.Now;

In the above example all the records which contains given ProductID and IsActive "true" records will be updated with IsActive "false" and UpdatedTimeStamp with current DateTime.

Submited By : Dotnet Learner On 04 Apr 2015

In entity framework code first approach, while creating the instance of database context it will automatically create the database. To avoid creation of database simple add the below one line code in Global.asax.cs Application_Start() event.

protected void Application_Start()

In the above example Models.SampleModeEntities is dbcontext name, change the value with your model dbcontext name.

Submited By : mrkraju On 18 Sep 2014