Three ways to update database fields from Https://www.cnblogs.com/cuihongyu3503319/p/8671006.htmlEntityFramework
Cases:
Entity class:
public class Testdbcontext:dbcontext {public dbset<test> Tests {get; set;} Public Testdbcontext (): Base () {} } public class Test {public long ID {get; set;} public string Name {get; set;} public string Email {get; set;} public string Remarks {get; set;} }
Create a database
Testdbcontext db = new Testdbcontext (); Db. Tests.add (new test () {Name = "Test 1", email= @ "[Email protected]", Remarks = "Test 1 Remarks"}); Db. Tests.add (new test () {Name = "Test 2", email = @ "[email protected]", Remarks = "Test 2 Remarks"}); Db. SaveChanges ();
The first method of updating data:
Query the records first, and then modify the corresponding properties. This method, although a query step, but also the use of EF automatic tracking function, the subsequent operation is more convenient.
For example, the generated SQL statement will only modify the corresponding modified fields.
And the test found that if the entity attribute value is not changed, the SQL statement will not be generated, for example, the following code is executed two times, the second SaveChanges () method does not execute the SQL UPDATE statement (NOTE: Because the query is still executed, so the efficiency of the operation is not significantly improved)
Testdbcontext db = new Testdbcontext (); var test = db. Tests.find (1); Test. Remarks = "Update field Method 1"; Db. SaveChanges ();
The second method:
Create a new entity class directly, and then modify the state of the entity object. Although this method is missing a query step, the resulting SQL statement modifies all fields and also ensures that the ID value exists. (Note: Fields that are not set are set to NULL because all fields are modified)
Testdbcontext db = new Testdbcontext (); Test test = new Test () {ID = 1, Remarks = "Update Field Method 2"}; Db. Entry (Test). state = System.Data.Entity.EntityState.Modified; Db. SaveChanges ();
The third method:
Creating an entity class, attaching an entity to the database context, and then modifying the corresponding property value is identified as true, and the SQL statement generated by this method is the same as the first method, relatively concise, and less of a query step, but the subsequent operations are cumbersome because you want to manually set the modification identity for each property, and also make sure that ID value exists.
Testdbcontext db = new Testdbcontext (); Test test = new Test () {ID = 1, Remarks = "Update Field Method 3"}; Db. Tests.attach (test); Db. Entry (Test). Property ("Remarks"). IsModified = true; Db. SaveChanges ();
Generated SQL statement (using SQL Server Profiler to track the database): The first method: (Note: The first method of code execution once again executes the SQL statement that did not track to the update database)
exec sp_executesql N ' UPDATE [dbo]. [Tests] SET [Remarks] = @0where ([ID] = @1) ', N ' @0 nvarchar (max), @1 bigint ', @0=n ' Update field Method 1 ', @1=1
The second method: (Note: To view the SQL statement, use this method if you are not aware of the possibility that the data will be modified incorrectly)
exec sp_executesql N ' UPDATE [dbo]. [Tests] SET [Name] = null, [Email] = null, [Remarks] = @0where ([ID] = @1) ', N ' @0 nvarchar (max), @1 bigint ', @0=n ' Update field Method 2 ', @1=1
The third method of
exec sp_executesql N ' UPDATE [dbo]. [Tests] SET [Remarks] = @0where ([ID] = @1) ', N ' @0 nvarchar (max), @1 bigint ', @0=n ' Update field Method 3 ', @1=1
EntityFramework three ways to update database fields (review)