Written by:2013/04/09 09:53 AM
Entity Framework 5 is the latest release of Microsoft ADO.Net data access. As with all data access frameworks, the prime purpose is to access and manipulate data. Entity Framework would be useless as a data access framework if it could not do so.
In my previous post on Entity Framework and CRUD operations I go through Create, Retrieve, Update and Delete operations using Linq to Entities and Entity Framework. In this article I will show how with Entity Framework we can perform multiple CRUD operations at once, allowing Entity framework to decide what to do, how to do it, and which operation to do first.
The challenge is that you have to update a row, create a new row and also delete a row. Traditionally we think of all three of these operations independently and rightly so. But also we would update the database independently three times. But in Entity Framework we are not working against the database so there is no need to think in database terms. So with Entity Framework 5 we can perform all three operations and only call the save changes once. By doing this we leave all the database decisions to Entity Framework.
First we will find a row and then update it
Customer cust = GetCustomer(30126);
cust.LastName = "CrudSurname";
Without saving to the database, we will now delete a row
var delCustomer = GetCustomer(30136);
if (delCustomer != null)
Then without saving the data again, we add a new row
Customer cust2 = new Customer();
cust2.CompanyName = "Crud Add Company Name";
cust2.EmailAddress = "firstname.lastname@example.org";
cust2.FirstName = "FirstName";
cust2.LastName = "LastName";
cust2.ModifiedDate = DateTime.Today;
cust2.NameStyle = false;
cust2.rowguid = Guid.NewGuid();
cust2.PasswordHash = "L/Rlwxzp4w7RWmEgXX+/A7cXaePEPcp+KwQhl2fJL7w=";
cust2.PasswordSalt = "1KjXYs4=";
cust2.Suffix = "JR";
//Add to dbcontext
Now we call save changes once and Entity Framework takes care of update, deleting and adding all the data for us.
Is there some magic happening here that we are not aware of? Well not really. Remember that we are not coding against a database, so do not think in database terms. Think in data model terms, in classes, entities and collections. Another important thing to remember is that the dbContext SaveChanges is wrapped in a transaction. So if one operation fails they all fail. Let's look the SQL to see how Entity Framework handles the SQL.
-- Region Parameters
where (([CustomerID]=@1) and ([LastName]
where (([CustomerID]=@0) and ([LastName]
'Crud Add Company Name'
insert[SalesLT].[Customer]([NameStyle], [Title], [FirstName], [MiddleName], [LastName], [Suffix], [CompanyName], [SalesPerson], [EmailAddress], [Phone], [PasswordHash], [PasswordSalt], [rowguid], [ModifiedDate])
values (@0, null, @1, null, @2, @3, @4, null, @5, null, @6, @7, @8, @9)
where@@ROWCOUNT> 0 and
Baring the SQL statements that retrieve the correct rows to delete and update we can see that Entity Framework actually creates the three SQL statements for us. By doing we do not have to deal with or worry about the actual SQL that will eventually accomplish the task.
0 comment(s) so far...
Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.
The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.
Subscribe to The Brave Programmer by Email
Sign up with Disqus to enjoy a surprise box of features