Entity FrameWork addition, deletion, query, modification, entityframework

Source: Internet
Author: User

Entity FrameWork addition, deletion, query, modification, entityframework

Add

 

1 # Add + void Add () 2 in region 1.0 /// <summary> 3 // Add 4 /// </summary> 5 static void Add () 6 {7 // 1. create an object and use the object initialization tool to initialize the object 8 Studnet stu = new Studnet () 9 {10 // assign the field name 11 s_Name = "Liu Dehua ", 12 s_Sex = "male", 13 s_Age = c_ID = 16}; 17 18 // 2. create an EF data context object and add it to the database through EF. 19 DB_USERSEntities db = new DB_USERSEntities (); 20 21 22/3 Add the object stu to 23 db in the Student set of the data context. studnet. add (stu); 24 25 // 4. call the data context storage method to save the object to the database 26 db. saveChanges (); 27 28 // ------ test code -------------- 29 Console. writeLine ("added successfully"); 30 31} 32 # endregionAdd
Modify

 

The modification is intelligent. When you generate an SQL statement, you only generate an SQL statement for the field of a modified column. You can use the SQL Server profiler tool in the SQLServer toolbar to listen to it.

1 # modify region 2.0 (query before modification) 2 // <summary> 3 // modify -- (for modification, according to the official standard, the data to be modified is first queried and then modified.) 4 // </summary> 5 static void Modify () 6 {7 // create the data context object 8 DB_USERSEntities db = new DB_USERSEntities (); 9 10 // FirstOrDefault the first element of the returned result 11 Studnet stu = db. studnet. where (s => s. s_ID = 10 ). firstOrDefault (); 12 13 Console. writeLine (string. format ("the information before modification is {0}:", stu. toString (); 14 15 // modify data 16 stu. s_Name = "Zhang huimei"; 17 18 // save it to database 19 db. saveChanges (); 20 21 // ------ test code ---------------------------------- // 22 Console. writeLine ("modified successfully"); 23 Console. writeLine (string. format ("the modified information is {0}:", stu. toString (); 24} 25 26 # endregionModify
Query

 

// The query result is an object, which is similar to system. What. This is because the ToString () method is the Object method. here we need to rewrite a Tostring method for the Student Object.
// Because the T4 template will automatically overwrite the previous changes and regenerate each time it is saved. Therefore, you need to write a partial category for student and rewrite the Tostring () method in the partial category.

1 # region 3.0 simple Query 2 // <summary> 3 // Query 4 /// </summary> 5 static void Query () 6 {7 // create the data context object 8 DB_USERSEntities db = new DB_USERSEntities (); 9 10 // query 11 List <Studnet> stu = db. studnet. where (s => s. s_Name = "Andy Lau "). toList (); 12 13 // ------ test code ------------------------------------------ // 14 // traverse the queried data 15 stu. forEach (s => Console. writeLine (s. toString (); 16} 17 # endregionQuery
Delete

 

 

// Deletion is also the same as modification. You need to first find the data to be deleted and then delete it, but this is not the case here.

1 # region 4.0 Delete 2 /// <summary> 3 // Delete 4 /// </summary> 5 static void Delete () 6 {7 DB_USERSEntities db = new DB_USERSEntities (); 8 9 // test the deletion of specified data (create the object to be deleted) 10 Studnet stu = new Studnet () {s_ID = 8}; 11 // attach to 12 db in EF. studnet. attach (stu); 13 // mark as deleting 14 db. studnet. remove (stu); 15 // Delete 16 db. saveChanges (); 17 18 // -------- test code --------------- // 19 Console. writeLine ("deleted successfully"); 20 21} 22 # endregionDelete

 

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.