Entity Framework study continued on the first day

Source: Internet
Author: User

Rewrite the addition, deletion, modification, and query method on the first day to observe the nature of addition, deletion, modification, and query.

1 using System; 2 using System. collections. generic; 3 using System. data. entity. infrastructure; 4 using System. linq; 5 using System. text; 6 using System. threading. tasks; 7 8 namespace EFConsole 9 {10 class Program11 {12 public static remotingEntities dbContext = new remotingEntities (); 13 static void Main (string [] args) 14 {15 // addOtherMethod (); 16 17 // updateOtherMethod (); 18 // removeOtherMethod (); 19 Console. readKey (); 20} 21 /// <summary> 22 // another method for adding 23 /// </summary> 24 public static void addOtherMethod () 25 {26 int res =-1; 27 user tempData = new user () {id = "123", name = "EntityEntry"}; 28 DbEntityEntry <user> entry = dbContext. entry <user> (tempData); 29 entry. state = System. data. entityState. added; 30 res = dbContext. saveChanges (); 31 Console. writeLine (res> 0? "Added successfully" + tempData. name: "failed to add "); 32} 33 // <summary> 34 // another modification method 35 // </summary> 36 public static void updateOtherMethod () 37 {38 int res =-1; 39 user tempData = new user () {id = "123", name = "update EntityEntry "}; 40 DbEntityEntry <user> entry = dbContext. entry <user> (tempData); 41 entry. state = System. data. entityState. unchanged; 42 entry. property ("name "). isModified = true; 43 res = dbContext. saveChanges (); 44 Console. WriteLine (res> 0? "Modification successful": "modification failed "); 45} 46 // <summary> 47 // another deletion method 48 /// </summary> 49 public static void removeOtherMethod () 50 {51 int res =-1; 52 user tempData = new user () {id = "123"}; 53 DbEntityEntry <user> entry = dbContext. entry <user> (tempData); 54 entry. state = System. data. entityState. deleted; 55 res = dbContext. saveChanges (); 56 Console. writeLine (res> 0? "Deletion successful": "deletion failed"); 57} 58} 59}View Code

Execute add, modify, and delete in sequence. The result is displayed.

The official recommendation is to first find the object from the database and then perform the modification operation when modifying the object data. However, we always think that this method is not very good and is not very efficient. So we need to find a better method.

We can regard the dbContext context as a container. When we use it to query the data in the database, it will maintain the data and put it in the container, but at this time the data is the proxy data, rather than our real data entities. Only in this way can the efficiency be improved. We can open the database to track the SQL statements issued by ef. When multiple fields of an object are modified, the SQL statement only modifies a field. For example, if the user includes the name, age, and other fields and uses the officially recommended method, we want to modify the data with id = 123 and query it first, in this way, all attributes of this object have values. When we only modify the name attribute and execute saveChange, only the name is updated in the SQL statement update, how can this problem be achieved without updating the values of other fields? Yes, this is the role of the proxy class mentioned above. We assume that the proxy class places a flag for each attribute. If the value of this attribute has not been modified, the flag is false; otherwise, the flag is true. When we update the proxy class, the context can generate SQL statements based on the flag, and check the modification method entry in the code. property ("name "). isModified = true; by setting the flag of the name attribute, the notification context changes the name attribute, so only the name field value is updated in the generated SQL statement. The same is true for deletion and addition. We will add or delete the notification context. Is efficiency greatly improved? By the way, the saveChanges in the launch text is also a way to improve efficiency. This method can be processed in batches. That is to say, when multiple objects are added, the connection is connected only once and multiple statements are executed, you can check the database. In fact, svaeChanges is used to traverse each data proxy object in the online text, to see which object status is the addition, deletion, and modification method, generate an SQL statement, and then execute it at one time.

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.