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 ();