EF addition, deletion, query, and modification (iii) ------ release version, ef release version

Source: Internet
Author: User

EF addition, deletion, query, and modification (iii) ------ release version, ef release version

1. Add

1 # Add student information in region 1.1 (defined as Int type, and the number of affected rows is returned) 2 /// <summary> 3 // Add student information 4 /// </summary> 5 /// <param name = "stu"> </param> 6/ // <returns> </returns> 7 public int Add (Studnet stu) 8 {9 // Add the object to the EF context and obtain the status management object 10 DbEntityEntry <Studnet> entry = db. entry <Studnet> (stu); 11 12 // change the status to Added13 // introduce the System. data. entity14 entry. state = System. data. entityState. added; 15 16 // save to database 17 return db. saveChanges (); 18 19} 20 # endregion

2. Delete

2.1 Delete based on user ID

1 # region 2.0 Delete 2 // <summary> 3 // Delete 4 /// </summary> 5 // <param name = "id"> </param> 6 // <returns> </returns> 7 public int Delete (int id) 8 {9 Studnet stu = new Studnet () {s_ID = id}; 10 // Delete the passed ID11 // DbEntityEntry <Studnet> stu = db. studnets. where (s => s. s_ID = id) as DbEntityEntry <Studnet>; 12 DbEntityEntry <Studnet> entry = db. entry <Studnet> (stu); 13 // change the status to deleted14 entry. state = System. data. entityState. deleted; 15 16 // save to database 17 return db. saveChanges (); 18} 19 # endregion

2.2 Delete based on conditions

Actually, a lambda expression is passed in the where method, and the lambda expression is essentially an anonymous function.

1 # region 2.1 Delete 2 /// <summary> 3 // Delete 4 according to the conditions (any conditions are given and then deleted) 5 /// </summary> 6 /// <returns> </returns> 7 public int DeleteBy (System. linq. expressions. expression <Func <Studnet, bool> deleteWhere) 8 {
// Query the data to be deleted 9 List <Studnet> stuList = db. studnets. where (deleteWhere ). toList (); 10 // Delete 11 stuList cyclically. forEach (u => db. studnets. remove (u); 12 13 // save to database 14 return db. saveChanges (); 15 16} 17 # endregion

3. Modify

1 # modify region 3.0 2 /// <summary> 3 // modify 4 // notify the program of the attribute changed. Add a variable parameter array 5 /// </summary> 6 /// <param name = "stu"> </param> 7 /// <returns> </returns> 8 public int Modify (Studnet stu, params string [] parmeters) 9 {10 // Add the object to the EF container and obtain the status management object 11 DbEntityEntry <Studnet> entry = db. entry <Studnet> (stu); 12 13 // The object state is changed to Unchanged14 entry. state = System. data. entityState. unchanged; 15 foreach (string parms in parmeters) 16 {17 entry. property (parms ). isModified = true; 18} 19 20 return db. saveChanges (); 21} 22 # endregion

Test modification:

1 /// <summary> 2 // modified test 3 /// </summary> 4 public Student () 5 {6 // change the name of the student whose Id is 1 to xxxxxxxxxxx 7 Studnet stu = new Studnet () {s_ID = 1, s_Name = "xxxxxxxxxxx "}; 8 // modify the object and attribute 9 this. modify (stu, "s_Name"); 10}Test Modify

Test deletion:

1 // <summary> 2 // Delete Test 3 /// </summary> 4 public Student () 5 {6 7 this. deleteBy (u => u. s_ID = 1); 8}Test Delete

4. Query

4.1 query by condition

1 # region 4.1 query by condition 2 // <summary> 3 // query by condition 4 // the query results are generally set, therefore, the return value type is generic set 5 /// </summary> 6 /// <returns> </returns> 7 public List <Studnet> GetStudentList (System. linq. expressions. expression <Func <Studnet, bool> whereQuery) 8 {9 return db. studnets. where (whereQuery ). toList (); 10} 11 # endregion

4.2 grouping is required after the query is completed based on the condition.

1 // <summary> 2 // query by condition, group 3 more /// </summary> 4 /// <param name = "whereQuery"> </param> 5 /// <param name = "QroupByQuery"> </ param> 6 // <returns> </returns> 7 public List <Studnet> GetStudentList <Tkey> (System. linq. expressions. expression <Func <Studnet, bool> whereQuery, System. linq. expressions. expression <Func <Studnet, Tkey> QroupByQuery) 8 {9 return db. studnets. where (whereQuery ). orderBy (QroupByQuery ). toList (); 10}

 

Test query by condition:

1 public Student () 2 {3 this. GetStudentList (u => u. s_ID = 1 & u. s_Sex = ""); 4 5}Query by condition

Test query by condition. After query, group by condition:

1 public Student () 2 {3 4 this. GetStudentList (u => u. s_ID> = 1, u => u. s_Sex); 5}Query and group

4.3 paging Query

1 # region 4.2 query by page 2 // <summary> 3 // query by page 4 // sort first, page 5 /// </summary> 6 /// <param name = "pageIndex"> page number </param> 7 /// <param name = "pageSize"> capacity </param> 8 /// <param name = "orderLambda"> sorting condition </param> 9 /// <returns> </returns> 10 public List <Studnet> getPagedList <Tkey> (int pageIndex, int pageSize, Expression <Func <Studnet, Tkey> orderLambda) 11 {
// Pagination: You must note that OrderBy is required before Skip, because a RowNum paging query will be generated (which is listened through SQL ServerProfier) 12 return db. studnets. orderBy (orderLambda ). skip (pageIndex-1) * pageSize ). take (pageSize ). toList (); 13} 14 # endregion

 

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.