Entity Frameworkの4.0でのオブジェクトの追加、更新、削除

前回の記事では、我々はする方法を説明しました  Entity Frameworkのモデルを作成し、複数のプロジェクトでそれを使用してくださいこの記事では、我々は、更新を追加し、私たちの概念モデル内のオブジェクトを削除する方法について説明しますと、基礎となるデータベースに変更をプッシュ。

我々は以前の記事で作成したコンソールアプリケーションを使用して、あなたが読んでいることを確認します  前の記事を  我々は続行する前に。私たちは、コンソールアプリケーションに3つのメソッドを追加していく予定:addCustomer()、deleteCustomer()とmodifyCustomer()

エンティティフレームワーク4.0でオブジェクトを作成し、追加

エンティティセットに新しいオブジェクトを追加するには、エンティティ型のインスタンスを作成する必要がありますし、オブジェクトコンテキストにオブジェクトを追加します。オブジェクトコンテキストに添付オブジェクトは、そのオブジェクトのコンテキストによって管理されています。今すぐオブジェクトコンテキストに新しいオブジェクトを追加するには、3つの方法があります。

1. objectSetと<TEntity> .AddObject() 
2. ObjectContext.AddObject() 
3. EntityCollection <TEntity> .Add()

私たちは、すなわちobjectSetと<TEntity> .AddObject()最初のメソッドを使用することになります。以下に示すコードを遵守。 

EF4は、エンティティを追加します。

ここで行っているすべてはお客様のタイプの新しいインスタンスを作成し、そのプロパティを移入です。私たちは、その後、お客様に、このインスタンスを追加するのEntitySet <T> .AddObjectメソッドを使用して設定。AddObjectメソッド()メソッドは、データベースに存在しない新たに作成されたオブジェクトを追加するために使用されます。AddObjectメソッド()が呼び出されると、一時的なのEntityKeyが生成され、以下に示すようにEntityStateは、「追加しました」に設定されています。 

エンティティの状態を追加しました 

context.SaveChanges()が呼び出されると、EF 4.0は先に行くと、データベースにレコードを挿入します。Entity Frameworkのは、データベースが理解クエリに我々のコードを変換し、すべてのデータの相互作用と低レベルの詳細を処理することに注意してください。また、我々は、オブジェクトやプロパティなどのデータにアクセスしていることを、上記のコードに注意してください。

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: 

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更新エンティティ

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: 

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’ 

エンティティの状態が削除 

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は、エンティティを削除します。 

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.

 全体の  ソースコード  この記事のオーバーダウンロードすることができ  、ここで

ます。https://www.cnblogs.com/zhangchenliang/archive/2013/01/05/2846245.htmlで再現

おすすめ

転載: blog.csdn.net/weixin_34187822/article/details/93495317