Entity Framework Extended Library (EF extension class Library, supporting batch update, deletion, and merging of multiple queries)

Source: Internet
Author: User

Today, at first glance, there are no articles about this class library in the garden. It is really surprising that there are many articles about the use of EF.

E good can directly look at the https://github.com/loresoft/EntityFramework.Extended

You can also directly install this package on nuget. The description is outdated. The latest version has switched to the IQueryable <T> extension, instead of DbSet <T> (which has been marked as a waste), you can use it if you have the same isolation as me. The following describes how to delete, update, and query objects in batches.

Batch Delete

We need to delete it like this.

// EF native deletion requires that the entity be removed first and then removecontext. remove (context. users. first (u => u. key = xxx); // if you want to delete more foreach (var user in context. users. where (u => u. firstName = "firstname "). toList () {context. remove (user );}

The problem that can be solved by an SQL statement has become complicated.

ORM is used to decouple SQL as much as possible, and more errors can be checked during compilation, but the above writing is confusing, if you have the feeling that the following statement is what you want in your mind.

 
 
---- It takes only one time to delete EF Extend Libary after it is referenced, which is much more efficient and does not require too many connection resources, making it easier to use.
// Delete all users where FirstName matchescontext. users. delete (u => u. firstName = "firstname"); // Of course, if I write it like this, context can be used. users. where (...). delete ();
When I first saw ef el, I was attracted by this writing method. Isn't that what we are always looking.
Batch update
// Set the salary to 999context for users whose usernames contain uppercase J in batch update. users. update (u => u. name. contans ("J"), u2 => new User {Salary = 999}); // The first parameter can also be passed into the existing IQuaryable parameter as follows var users = context. users. where (u => u. firstName = "firstname"); context. users. update (users, u => new User {FirstName = "newfirstname "});

// Of course, this is my favorite method.
Context. Users. Where (u => u. FirstName = "firstname"). Update (u => new User {FirstName = "newfirstname "})

Do you prefer this extension library? I can't help it anymore, but it's a pity that I have started using it now.

Batch query

In fact, the current query is already great, and the default latency query can meet basic requirements, but sometimes it is always expected to be more extreme. For example, the existing query cannot meet the stubborn requirement of paging.

// See how ef el solves the problem. // query var q = db. tasks. where (t => t. priority = 2); // obtain the total number of var q1 = q. futureCount (); // obtain the paging data var q2 = q. skip (pageIndex ). take (pageSize ). future (); // the query in all the previous Future functions is encapsulated into a connection and int total = q1.Value is executed. // The result is obtained, var tasks = q2.ToList () is not queried again here ();

  

 

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.