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

Article translations Article translations
Article ID: 2802240 - View products that this article applies to.

About Author:

Collapse this tableExpand this table
Collapse this imageExpand this image
2401266
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.
Expand all | Collapse all

On This Page

Summary

This article covers various delete methods by using Entity Framework 5.0

This article covers ways to delete the rows / records through Entity Framework. We start with deleting a single record. In this article the control mechanism has been added to check whether there is a record available for deletion.



More information

Delete single records

The code below passes in a product id as the argument to delete a particular product with the specified product id.



public static void Delete(int Id)
{
northwindEntities ctx = new northwindEntities();
var result = from r in ctx.Products where r.ProductID == Id select r;
//check if there is a record with this id

if (result.Count() > 0)

{
Product product = result.First();
ctx.Products.Remove(product);
ctx.SaveChanges();
}
}


Bulk record delete

The code below does a bulk delete based on the category id to all the products.



public static void DeleteSelectedRecord(int Id)
{
northwindEntities ctx = new northwindEntities();
var result = from r in ctx.Products where r.CategoryID == Id select r;
//check if there is a record with this id

 if (result.Count() > 0)

{

foreach (Product p in result)
 {
ctx.Products.Remove(p);
}
ctx.SaveChanges();
}
}


Bulk record update with SQL statement

The follow code does bulk delete 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 BulkDeleteSQL()
{
northwindEntities ctx = new northwindEntities();
ctx.Database.ExecuteSqlCommand("DELETE Products");
}



References

For more information, see CRUD using Entity Framework in .NET Framework 5.0 - (Create), CRUD using Entity Framework in .NET Framework 5.0 - (Read) and CRUD using Entity Framework in .NET Framework 5.0 - (Update).

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: 2802240 - Last Review: April 1, 2013 - Revision: 2.0
Applies to
  • Microsoft Visual Studio 2012 Beta
  • 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 Test Professional 2012
  • Microsoft Visual Studio Ultimate 2012
Keywords: 
KB2802240

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com