Tuesday, December 4, 2012

Deleting Data using ADO.Net Entity Framework

In order to select data from a Model using the ADO.Net Entity Data Model, we should first create an ADO.Net Entity Data Model class, refer to the post Creating your First ADO.Net Entity Data Model to see on how to create an ADO.Net Entity Data Model, that maps to the physical database.

Once you have created the ADO.Net Entity Data Model class, we can proceed with querying the model to select data. In this post we shall see on how to use the ADO.Net Entity Data Model to DELETE a record in the Employee table.

1. Create an Object of the ADO.Net Entity Data Model class
2. Get the Object of the Employee whose details needs to be Deleted
3. Delete the Object
4. Commit changes to the Model

Here is the code to DELETE rows using the ADO.Net Entity Data Model

EmployeesEntities dbContext = new EmployeesEntities();
//
// Get The Object of the Selected Employee
Employee objEmployee = (from emp in dbContext.Employee
                        where emp.ID == 235
                        select emp).First();
//
// Delete the Employee Object and Save the Detils.
dbContext.DeleteObject(objEmployee);
dbContext.SaveChanges();

5. Once we execute the code, a record for the Employee with ID 235 will get deleted from the Employee Table.

ht: normal;mso-layout-grid-align:none;text-autospace:none'>objEmployee.Salery = 7500;
//
// Save the Employee Details

Search other Products:
Flipkart.com

No comments: