Entity FrameWork adds, deletes, queries, and modifies the essence, entityframework

Source: Internet
Author: User

Entity FrameWork adds, deletes, queries, and modifies the essence, entityframework

As mentioned in previous articles, EF has been added, deleted, checked, modified, and deleted. The modification and deletion at that time can only be the first query of the data to be modified, then modification and deletion... Now you can add, query, modify, and delete an existing version.

1. Add

1 static void Add () 2 {3 // 1. create a student object and use the object initialization tool to initialize this object 4 Studnet stu = new Studnet () 5 {6 s_Name = "", 7 s_Sex = "male ", 8 s_Age = 39, 9 c_ID = 2, 10 11}; 12 13 // 2. Add the object to EF and obtain the status management object of the current object 14 DbEntityEntry = db. entry <Studnet> (stu); 15 // 3. set the object status to Added (entry. state) 16 entry. state = System. data. entityState. added; 17 // 4. save to database 18 db. saveChanges (); 19 20 // test code 21 Console. writeLine ("added successfully"); 22}ADD

 

2. Modify

1 /// <summary> 2 // write and modify by yourself (optimize the modification method and directly modify the object created) 3 /// </summary> 4 static void Edit () 5 {6 7 // 1. query an object to be modified 8 Studnet stu = new Studnet () 9 {10 // 1. query the data to be modified. 11 // The database contains data with the s_ID of 12, change the name of the record s_ID = 12 to "!!! ", Change the gender to "female", and change the age to "23" 13 s_ID = 12,14 s_Name = "!!! ", 15 s_Sex =" female ", 16 s_Age =}; 18 19 // 2, add the object to the EF container, and get the status management object of the current object 20 DbEntityEntry entry = db. entry <Studnet> (stu); 21 // 3. this object has not been modified. Unchanged.22 entry. state = System. data. entityState. unchanged; 23 // 4. set the modified attribute 24 entry. property ("s_Name "). isModified = true; 25 entry. property ("s_Sex "). isModified = true; 26 entry. property ("s_Age "). isModified = true; 27 28 // 5. save the database 29 db. saveChanges (); 30 31 // --- Test 32 Console. writeLine ("modified successfully"); 33 34}Modify

 

3 Delete

1 /// <summary> 2 /// Delete 3 /// </summary> 4 static void Delete () 5 {6 7 // 1 create the object to be deleted, here is just a test. Delete the data with Id = 15 8 Studnet stu = new Studnet () {s_ID = 15}; 9 10 // 2 Add the object to the EF container, and get the status management object of the current object 11 DbEntityEntry <Studnet> entry = db. entry <Studnet> (stu); 12 13 14 // 3. change the object status to Deleted15 entry. state = System. data. entityState. deleted; 16 17 // 4 Save the data to the database 18 db. saveChanges (); 19 // test with ----------------------- 20 Console. writeLine ("deleted successfully"); 21}Delete

 

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.