• Add, Update and Delete Objects in Entity Framework 4.0


    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: 

    EF4 Add Entity

    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: 

    Entity State Added 

    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: 

    Entity Added In DB 

    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. 

    EF4 Update Entity

    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: 

    Entity Update In DB 

    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’ 

    Entity State 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 

    EF4 Delete Entity 

    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.

  • 相关阅读:
    希尔排序
    Java内存区域与内存溢出异常
    插入排序
    选择排序
    冒泡排序
    专利申请笔记
    Python基础指北
    mini web
    linux i/o multiplexing
    Python decorator module
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2846245.html
Copyright © 2020-2023  润新知