How to modify an object in ADO. NET EF

Source: Internet
Author: User

1. For the traditional modification mode, check the following code:
Copy codeThe Code is as follows:
Using (NorthwindEntities context = new NorthwindEntities ())
{
Region region = context. Region. FirstOrDefault (v => v. RegionID = 4 );
Region. RegionDescription = "Test ";
Context. SaveChanges ();
}



Monitoring SQL statement:
Copy codeThe Code is as follows:
SQL1: select top 1 [Extent1]. [RegionID] AS [RegionID], [Extent1]. [RegionDescription] AS [RegionDescription] FROM [dbo]. [Region] AS [Extent1] WHERE 4 = [Extent1]. [RegionID]
SQL2: exec sp_executesql n' update [dbo]. [Region] set [RegionDescription] = @ 0 where ([RegionID] = @ 1) ', n' @ 0 nchar (4), @ 1 int ', @ 0 = N 'test', @ 1 = 4


From the example here, we can see that to update data in the traditional mode, you must first execute a query to obtain the object to be updated. See the following example:
Copy codeThe Code is as follows:
Region region;
Using (NorthwindEntities context = new NorthwindEntities ())
{
Region = context. Region. FirstOrDefault (v => v. RegionID = 4 );
}
Using (NorthwindEntities context = new NorthwindEntities ())
{
Region. RegionDescription = "Test ";
Context. SaveChanges ();
}



Update Is Not executed because the entity no longer executes the SaveChanges object. Therefore, when we update an object that is no longer in the current connection, we must first execute a query to obtain the object to update it, as shown below:
Copy codeThe Code is as follows:
Region region;
Using (NorthwindEntities context = new NorthwindEntities ())
{
Region = context. Region. FirstOrDefault (v => v. RegionID = 4 );
}
Using (NorthwindEntities context = new NorthwindEntities ())
{
Region newRegion = context. Region. FirstOrDefault (v => v. RegionID = region. RegionID );
Region. RegionDescription = "Test ";
Context. SaveChanges ();
}


2. Use ApplyPropertyChanges to modify an object
Copy codeThe Code is as follows:
Region region;
Using (NorthwindEntities ne = new NorthwindEntities ())
{
// Using EntityObject. Execute (MergeOption. NoTracking) is equivalent to using ObjectContext. Dettach (EntityObject)
// Query and separate objects
Region = ne. Region. Execute (MergeOption. NoTracking). Where (v => v. RegionID = 1). FirstOrDefault ();
}
// Modify the value of separation
Region. RegionDescription = "TestTest1 ";
// Use the separated object order to update
Using (NorthwindEntities context = new NorthwindEntities ())
{
// Load the data into the context for updates
Context. GetObjectByKey (region. EntityKey );
// Use order to update the corresponding object in the context
Context. ApplyPropertyChanges (region. EntityKey. EntitySetName, region );
Context. SaveChanges ();
}

Monitoring SQL statement:


Copy codeThe Code is as follows:
SQL1: exec sp_executesql n' SELECT [Extent1]. [RegionID] AS [RegionID], [Extent1]. [RegionDescription] AS [RegionDescription] FROM [dbo]. [Region] AS [Extent1] WHERE [Extent1]. [RegionID] = @ p0 ', n' @ p0 int', @ p0 = 1
SQL2: exec sp_executesql n' update [dbo]. [Region] set [RegionDescription] = @ 0 where ([RegionID] = @ 1) ', n' @ 0 nchar (9), @ 1 int ', @ 0 = N 'testtest1', @ 1 = 1



In MSDN, ApplyPropertyChanges explains "apply the property changes of separated objects to the objects attached to the object context ." To put it bluntly, we use the old object to update the new object. We can see that using the "ApplyPropertyChanges Modify Object" method to modify the object is the same as using the "traditional mode, you must first perform a query to obtain the updated object. However, the ApplyPropertyChanges method takes the objects in memory (new objects) and the objects in the current connection (old objects) in comparison, the Update statement for modifying the corresponding field is automatically generated. If the objects in the memory are exactly the same as the objects in the current connection (the values of each field are equal), no response Update is generated. When we execute the above Code again and observe that the SQL statement is monitored, you will find that only SQL1 is monitored and SQL2 is not obtained.

3. Use Attach and SetModifiedProperty to modify the object
Copy codeThe Code is as follows:
Using (NorthwindEntities context = new NorthwindEntities ())
{
Region region = context. Region. FirstOrDefault (v => v. RegionID = 4 );
Context. Detach (region );
Region. RegionDescription = "test ";

Context. Attach (region );
Var newRegion = context. ObjectStateManager. GetObjectStateEntry (region );
NewRegion. SetModified ();
NewRegion. SetModifiedProperty ("RegionDescription ");

Context. SaveChanges ();
}

SQL statement monitoring:

Copy codeThe Code is as follows:
Exec sp_executesql n' update [dbo]. [Region] set [RegionDescription] = @ 0 where ([RegionID] = @ 1) ', n' @ 0 nchar (4), @ 1 int ', @ 0 = n' test ', @ 1 = 4

By using this method, you can add objects that are no longer in the current connected set to the current set using the Attach method, and set and modify fields using SetModifiedProperty, this method can be modified without the need to query and read data into the current connection object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.