Create an Entity Framework Model and Use it in Multiple Projects
来源:互联网 发布:大数据建设是什么 编辑:程序博客网 时间:2024/06/18 05:34
We will be using the same console application that we created in the previous article, so make sure you have read theprevious 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 myEntity Framework 4.0 article series.
The entire source code of this article can be downloaded over here.
- Create an Entity Framework Model and Use it in Multiple Projects
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
- How to create a DLL library in C and then use it with C#
- Entity Framework Model First
- Entity Framework Model First
- [ASP.NET MVC5 - 03] Entity FrameWork and Model
- Insider Computer Fraud: An In-depth Framework for Detecting and Defending against Insider IT Attacks
- Install And Use Laravel Framework In Fedora25
- Multiple Startup Projects in VS2008
- Templates and multiple-file projects
- Create custom dialogs for use in your Visual Studio Setup projects
- Entity Framework 6.1-Model First
- Entity Framework 6.1-Model First
- Create an apple style menu and improve it via jQuery
- How to map an Entity framework model to a table name dynamically
- install glm library in ubuntu and use it in qt
- install eigen in ubuntu and use it in qt
- Define enum in xml and use it in another xml
- Ceph集群配置——前言
- 杂谈
- MongoDB install and use
- C语言---整型字符串转换
- 面向过程和面向对象
- Create an Entity Framework Model and Use it in Multiple Projects
- 适用于 PHP 开发人员的 Python 基础知识
- 十大移动应用开发外包公司
- Creating a Tasklist Application with ASP.NET MVC
- [ASP.NET]解决“试图将 Null 值赋予一个非 Variant 数据类型的变量”的问题
- 22个所见即所得在线 Web 编辑器
- java String.split丢失结尾空字符串的问题
- 应用程序基础之03Manifest文件 (二)
- 动态创建BUTTON控件