Entity Framework Extended Library (EF extension class libraries, support batch update, delete, merge multiple queries, etc.)

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/jinzhao/archive/2013/05/31/3108755.html

E-Text good can see directly https://github.com/loresoft/EntityFramework.Extended

You can also install the package directly on NuGet, and the latest version has been replaced with an extension to iqueryable<t>. The following describes how to bulk delete, update, query.

Bulk Delete

We were supposed to delete this.

// EF Native Deletion requires removing the entity andremoving the context first. Remove (context. Users.first (u=>u.key==xxx); // If you want to delete more foreach (varin"firstname"). ToList ()) {context. Remove (user);

The original sentence of SQL can solve the problem, become complex.

ORM is designed to be decoupled from SQL as much as possible, and to be able to check for more errors at compile time, but the above is a bit of a block of panic, if you have this feeling the following wording is not what you want in your mind.

----引用EF Extend Libary后删除只需要一次就完成了,效率高了很多,也不需要太多的连接资源,使用更方便了

// Delete all users where FirstName matches " FirstName " ); // of course, if I write this can also context. Users.where (...). Delete ();

Batch Update
//Bulk Update user name with uppercase J set Payroll to 999context. Users.update (U= U.name.contans ("J"), U2=NewUser {Salary =999}); //The first parameter can also pass in an existing iquaryable parameter as followsvarUsers = context. Users.where (U = u.firstname = ="FirstName"); context. Users.update (Users, U=NewUser {FirstName ="Newfirstname"});<br><br>//Of course, my favorite is still this way of writing <br>context. Users.where (U = u.firstname = = "FirstName"). Update (u=>new user{firstname = "Newfirstname"})

Bulk Query

In fact, now the query is very good, the default delay query can meet the basic needs, but sometimes always want to be more extreme, such as the existing query can not meet the stubborn demand for paging.

//See how EF El solved//multiplexed QueriesvarQ = db. Tasks.where (t = t.priority = =2);//Get TotalvarQ1 =Q.futurecount ();//Get Paged DatavarQ2 =Q.skip (PageIndex). Take (pageSize). Future (); //This will trigger the query wrapped in all future functions above to be executed in a single connection .intTotal =Q1. Value;//because the results have been obtained, this will not be queried againvartasks = Q2. ToList ();

Learn to learn ~ ~

Entity Framework Extended Library (EF extension class libraries, support batch update, delete, merge multiple queries, etc.)

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.