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;
    });
    db.SaveChanges();
}

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.

Read More...
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.


Example:
protected void Application_Start()
{
    System.Data.Entity.Database.SetInitializer<Models.SampleModeEntities>(null);
}


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

Read More...
Submited By : mrkraju On 18 Sep 2014




Search dotnetlearners.com