Add, delete, query, modify, pagination, join ...... Join...

Source: Internet
Author: User

Add, delete, query, modify, pagination, join ...... Join...

1. Batch add data

1 static void Main (string [] args) 2 {3 add (); 4 add2 (); 5 Console. readKey (); 6} 7 8 static void add () 9 {10 DemoDbEntities db = new DemoDbEntities (); 11 Stopwatch st = new Stopwatch (); 12 st. start (); 13 for (int I = 0; I <1000; I ++) 14 {15 db. user. add (new User {NAME = "zhangsan" + I, AGE = I}); 16 db. saveChanges (); 17} 18 st. stop (); 19 Console. writeLine ("Time: {0} millisecond", st. elapsedMilliseconds); 20 21 22} 23 24 static void add2 () 25 {26 DemoDbEntities db = new DemoDbEntities (); 27 Stopwatch st = new Stopwatch (); 28 st. start (); 29 for (int I = 0; I <1000; I ++) 30 {31 db. user. add (new User {NAME = "James" + I, AGE = I}); 32 33} 34 db. saveChanges (); 35 st. stop (); 36 Console. writeLine ("Time: {0} millisecond", st. elapsedMilliseconds); 37 38}View Code

1 static void Main (string [] args) 2 {3 del (); 4 Console. readKey (); 5} 6 7 # region EF batch Delete 8 static void del () 9 {10 DemoDbEntities db = new DemoDbEntities (); 11 12 List <User> list = db. user. where (u => u. NAME = "Zhang San 1 "). toList (); 13 // Any indicates that if there is a value in the Set, true is returned. Otherwise, float 14 if (list! = Null & list. any () 15 {16 foreach (User item in list) 17 {18 db. user. remove (item); 19} 20 db. saveChanges (); 21} 22 Console. writeLine ("deleted successfully"); 23} 24 # endregionView Code

First query the results to ensure that there are values in the database, and then delete.

Iii. EF data editing

The first method is to edit the query;

The second method is to directly pass in a model object to be modified, which can be partial fields.

1 static void Main (string [] args) 2 {3 edit2 (); 4 Console. readKey (); 5} 6 # region EF edit data 7 static void edit () 8 {9 DemoDbEntities db = new DemoDbEntities (); 10 11 var model = db. user. firstOrDefault (u => u. NAME = "James 3"); 12 model. NAME = "Li Si"; 13 db. saveChanges (); 14 Console. writeLine ("edited"); 15} 16 static void edit2 () 17 {18 DemoDbEntities db = new DemoDbEntities (); 19 20 User model = new User () {21 ID = 4, 22 NAME = "" 23}; 24 // get the status of the proxy object class to Detaceh 25 System. data. entity. infrastructure. dbEntityEntry entry = db. entry (model); 26 // 1. Change the status of the proxy class to Unchanged 2. Change the IsModified of the field to be updated in the proxy class to true 27 entry. state = System. data. entity. entityState. unchanged; 28 entry. property ("NAME "). isModified = true; 29 // solution to an error in verifying one or more entities: Disable the entity validity check of EF by 30 db. configuration. validateOnSaveEnabled = false; 31 db. saveChanges (); 32 Console. writeLine ("edited"); 33} 34 # endregionView Code

Iv. Use of join in EF

1 static void Main (string [] args) 2 {3 efjoin2 (); 4 Console. readKey (); 5} 6 7 # two modes of region EF join Table query 8 static void efjoin () 9 {10 DemoDbEntities db = new DemoDbEntities (); 11 12 var SQL = db. user. join (db. groupInfo, u => u. groupinfoID, g => g. ID, (c, g) => new {uername = c. NAME, g. NAME}); 13 14 var list = SQL. toList (); 15 16 list. forEach (c => Console. writeLine (c. uername + "" + c. NAME); 17} 18 static void efjoin2 () 19 {20 DemoDbEntities db = new DemoDbEntities (); 21 22 db. user. include ("GroupInfo "). where (c => true ). toList (). forEach (c => Console. writeLine (c. NAME + "" + c. groupInfo. NAME); 23 24} 25 # endregionView Code

V. EF paging Query

1 static void Main (string [] args) 2 {3 fenye (); 4 Console. readKey (); 5} 6 7 # region EF page 8 static void fenye () 9 {10 DemoDbEntities db = new DemoDbEntities (); 11 // before paging, use OrderBy or OrderByDescending to forward or reverse the data, then skip the number of entries in skip (), and take () to query the number of entries. 12 db. user. orderBy (u => u. ID ). skip (0 ). take (5 ). toList (). forEach (c => Console. writeLine (c. ID); 13} 14 # endregionView Code

Vi. EF stored procedure call

1 static void Main (string [] args) 2 {3 cunchu (); 4 Console. readKey (); 5} 6 7 # region EF use 8 static void cunchu () 9 {10 DemoDbEntities db = new DemoDbEntities (); 11 // call the Stored Procedure USP_GetPagedArticleList 12 int count = 0; 13 // Since totalItems is an output parameter, the programmer defines 14 ObjectParameter ps = new ObjectParameter ("totalItems ", count); 15 16 db. USP_GetPagedArticleList (1, 2, ps ). toList (). forEach (u => Console. writeLine (u. ID); 17 18 Console. writeLine ("Total number of rows =" + ps. value); 19} 20 # endregionView Code

VII. Execute SQL in EF

1 static void Main (string [] args) 2 {3 EFtoSql (); 4 Console. readKey (); 5} 6 7 # Execute SQL statement 8 static void EFtoSql () 9 {10 DemoDbEntities db = new DemoDbEntities () in region EF (); 11 string SQL = "update [DemoDb]. [dbo]. [User] set NAME = @ name where ID> @ id "; 12 13 SqlParameter [] p = new SqlParameter [] {14 new SqlParameter (" @ id ", 5 ), 15 new SqlParameter ("@ name", "") 16}; 17 db. database. executeSqlCommand (SQL, p); 18 Console. writeLine ("modified successfully"); 19} 20 21 # endregionView Code

8. AsNoTracking

1 static void Main (string [] args) 2 {3 EFAsNoTracking (); 4 Console. readKey (); 5} 6 # region EF does not track and query AsNoTracking () 7 static void EFAsNoTracking () 8 {9 DemoDbEntities db = new DemoDbEntities (); 10 // use AsNoTracking () the query efficiency is improved, and 11 db is not cached in DbContext. user. asNoTracking (). where (u => u. ID> 5 ). toList (). forEach (c => Console. writeLine (c. ID); 12} 13 # endregionView Code

9. Use of the Set <T> generic method in the EF context container

1 static void Main (string [] args) 2 {3 EFSet (); 4 Console. readKey (); 5} 6 7 # role of the Set <T> generic method in the region EF context container 8 static void EFSet () 9 {10 DemoDbEntities db = new DemoDbEntities (); 11 // db. set <User> is equivalent to db. user 12 db. set <User> (). where (u => u. ID> 5 ). toList (). forEach (c => Console. writeLine (c. ID); 13} 14 # endregionView Code

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.