You are currently offline, waiting for your internet to reconnect

CRUD using Entity Framework in .NET Framework 5.0 - (Update)

About Author:

This article is provided by MVP Ming Man. Microsoft is so thankful that MVPs who proactively share their professional experience with other users. The article would be posted on MVP's website or blog later.

Summary

This article covers ways to update through Entity Framework. We start with updating a single record.



Update single records

The code below pass in a product id as the argument then update the product name based on the product id.

public static void Update(int Id)
{
northwindEntities ctx = new northwindEntities();

var result = from r in ctx.Products where r.ProductID == Id select r;

// Get the first record from the result
Product product = result.First();

// Update the product name
product.ProductName = "Drinks";

ctx.SaveChanges();
}


Bulk record update

The code below does a bulk update to the category id to all the products.

public static void BulkUpdate()
{
northwindEntities ctx = new northwindEntities();
var result = from r in ctx.Products select r;

foreach (Product p in result)
{
p.CategoryID = 1;
}
ctx.SaveChanges();
}


Bulk record update with SQL statement


The follow code does bulk update with SQL statement. The disadvantage of this is that the compiler is not able to check the syntax of the SQL statement.

public static void BulkUpdateSQL()
{
northwindEntities ctx = new northwindEntities();
ctx.Database.ExecuteSqlCommand("UPDATE Products SET CategoryID = 3");
}

Community Solutions Content Disclaimer

MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.
Properties

Article ID: 2796236 - Last Review: 04/01/2013 02:24:00 - Revision: 4.0

Microsoft Visual Studio Express 2012 for Web, Microsoft Visual Studio Express 2012 for Windows 8, Microsoft Visual Studio Express 2012 for Windows Desktop, Microsoft Visual Studio Express 2012 for Windows Phone, Microsoft Visual Studio Premium 2012, Microsoft Visual Studio Professional 2012, Microsoft Visual Studio Ultimate 2012

  • KB2796236
Feedback
r varAutoFirePV = 1; var varClickTracking = 1; var varCustomerTracking = 1; var Route = "76500"; var Ctrl = ""; document.write(" nguage in languagesListForLargeScreens track by $index -->
대한민국 - 한국어
España - Español
Paraguay - Español
Venezuela - Español
://c1.microsoft.com/c.gif?DI=4050&did=1&t=">-xxxxxxxxxxxx".replace(/x/g, function () { return Math.floor(Math.random() * 16).toString(16); })).replace("R", (8 | Math.floor(Math.random() * 3)).toString(16)); var m = document.createElement("meta"); m.content = guid; m.name = "ms.dqid"; document.getElementsByTagName("head")[0].appendChild(m);