Add, Update and Delete Objects in Entity Framework 4.0
Posted by: Suprotim Agarwal
in Category Entity Framework
Abstract: In this article, we will see how to Add, Update and Delete objects in our conceptual Entity model and push the changes to the underlying database.
In a previous article, we saw how to Create an Entity Framework Model and Use it in Multiple Projects. In this article, we will see how to Add, Update and Delete objects in our conceptual model and push the changes to the underlying database.
We will be using the same console application that we created in the previous article, so make sure you have read the previous article before we continue. We will be adding three methods to the console application: addCustomer(), deleteCustomer() and modifyCustomer()
Create and Add Objects in Entity Framework 4.0
In order to add new objects to the Entity Set, you must create an instance of an Entity type and add the object to the object context. Objects attached to the object context are managed by that object context. Now there are three ways to add a new object to the object context:
1. Use ObjectSet<TEntity>.AddObject()
2. Use ObjectContext.AddObject()
3. Use EntityCollection<TEntity>.Add()
We will be using the first method i.e. ObjectSet<TEntity>.AddObject(). Observe the code shown below:
All we are doing here is create a new instance of the Customer type and populate its properties. We then add this instance to the Customers set using the EntitySet<T>.AddObject method. The AddObject() method is used for adding newly created objects that do not exist in the database. When AddObject() is called, a temporary EntityKey is generated and the EntityState is set to 'Added', as shown below:
When context.SaveChanges() is called, EF 4.0 goes ahead and inserts the record into the database. Note that Entity Framework converts our code into queries that the database understand and handles all data interaction and low-level details. Also notice in the code above, that we are accessing data as objects and properties.
After you have executed the code, you can go ahead and physically verify the record in the database. If the query executed successfully, you should see a new record in the Customers table of the Northwind database, as shown below:
Update Objects in Entity Framework 4.0
As I mentioned earlier, objects attached to the object context are managed by that object context. The steps to update an existing entity are quite simple. First retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then edit the properties of the Entity and finally call SaveChanges() on the context.
In the code shown above, we are modifying the newly inserted record (‘ID=DNC’) we created in the previous step and then add the ‘CEO’ value for the ContactTitle column. When EF converts your code to a SQL query, it automatically uses parameters to avoid SQL injection attacks.
Execute the code and go ahead and check the record in the database:
Delete Objects in Entity Framework 4.0
To delete an existing record, retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then call the ObjectSet<TEntity>.DeleteObject() . When the DeleteObject() method is called, the EntityState of the object is set to ‘Deleted’
Finally call SaveChanges() on the context to delete the data from the data source. Another way to delete an object is to use the ObjectContext.DeleteObject() but we have used the ObjectSet.DeleteObject for our example
Go ahead and check the database and you will find that the CustomerID=DNC no more exists.
Well this was an overview of how we can Add, Delete and Update Objects in Entity Framework 4.0. We have not yet touched upon issues like Concurrency, ForeignKey Dependencies and Cascading Deletes, which is common while dealing with CRUD operations. I will be covering that in my future articles.
If you are interested in Entity Framework 4.0, I strongly suggest you to check out my Entity Framework 4.0 article series.
The entire source code of this article can be downloaded over here.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!