Introduction to Entity Framework (4)-entityframework. Extended

Source: Internet
Author: User

We have a rough understanding of EF operations, but in practice, it seems that some common functions are still lacking, that is, batch deletion and data update.

To undertake the above part, we have a database called House, which contains the House table and seller table.

1. How can I batch Delete and modify native Ef?

// Modify public static bool updateallprice (decimal price) {bool isok = false; using (houseentities DB = new houseentities () {var houselist = dB. house. tolist (); // first obtain the list of all houses (equivalent to select * From House, although it will actually replace * with all fields, which can improve performance) houselist. foreach (x => {X. price = price; // perform a foreach operation to change the price of each item to the imported price dB. entry (X ). state = system. data. entity. entitystate. modified; // mark the modified object as modified}); isok = dB. savechanges ()> 0; // if the modification quantity is greater than 0, return isok;} // Delete public static bool deletehousebyregion (string region) {bool isok = false; using (houseentities DB = new houseentities () {var houselist = dB. house. where (x => X. region = region ). tolist (); // first obtain the list of eligible houses houselist. foreach (x => {dB. entry (X ). state = system. data. entity. entitystate. deleted; // mark the modified object as deleted}); isok = dB. savechanges ()> 0; // if the modification quantity is greater than 0, the operation is successful.} return isok ;}

The preceding sample code can be seen. Whether it is batch deletion, modification of some data, or all data. First, you need to query and instantiate the operation object, and then use the Traversal method to modify and delete it.

This method seems very strange. I used to use SQL, delete house where region = 'xxx', or update House set price = 123.123 to implement the functions. Now I have wasted so much effort, it's just a waste of effort. EF also has a huge performance problem (it's just a waste of performance for every query, let alone maintaining the instance status, fortunately, EF will cancel the update operation on objects that are not updated, but it still seems to be worth the candle ).

The problem is obvious here. How can we solve it? You need to use an extremely useful extension "entityframework. Extended" in entityframework ". This is an open-source extension class library that can be supported only in. Net 4.0 or later versions. You can find it on nuget and GitHub. It uses the lambda Expression Tree to parse objects. After parsing, it directly generates SQL commands to complete database operations. Compared with traditional SQL statements, this not only makes our code more beautiful, but also gives us more peace of mind in terms of security and performance.

Ii. Install entityframework. Extended

Like other class libraries, you only need to search for nuget during installation. If you cannot find it, try to add 8.8.8.8 as the DNS server (if you cannot find it, wait, foreign websites are often walled, even normal websites ).

As shown in, the installation can be completed (what? You cannot find nuget? Well, I think my first article and the search box in the upper right corner of Vs will help you.

Iii. Use entityframework. extended for batch deletion and Modification

// Update public static bool updateallprice (decimal price) {bool isok = false; using (houseentities DB = new houseentities () {isok = dB in batches. house. update (x => new house () {price = Price})> 0; // The update House set price = xxx statement is generated directly and the number of affected rows is returned, here, X has no practical effect, but as a memberinitexpression, it is an indispensable part. Here, you must note that the object cannot be transmitted before the new object, because when capturing accurate fields, we need to use the init expression. If the object has been generated, all non-empty fields will be initialized, which is completely unnecessary for us. } Return isok;} // Delete public static bool deletehousebyregion (string region) {bool isok = false; using (houseentities DB = new houseentities () {isok = dB. house. where (x => X. region = region ). delete ()> 0; // batch deletion can be achieved by directly following the WHERE clause with Delete. Is it convenient? } Return isok ;}

The above code is written after the extended extension library is used. Not only is the whole code much more elegant, but it also achieves better performance. Before using the extension library, do not forget to reference the entityframework. Extensions namespace. The extension class is not automatically reminded by the SDK.

Have you found out? As I said before, modify, delete, and add the savingchanges () operation. It is not written here. Is it wrong? Of course not. The extension library directly generates SQL statements and uses ADO.. Net method, so you do not need to write savingchanges. After these extension methods are executed, the affected number of rows will be returned.

Introduction to Entity Framework (4)-entityframework. Extended

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.