Ways to improve EF code (below)

Source: Internet
Author: User

In this section, we'll cover some of the ways to improve EF code, including compiling queries, storing model views, and conflict handling.

> CompiledQuery

Provides compilation and caching of queries for reuse. When the same query needs to be executed many times, we can use Compliequery to compile the query's statements for the next use, eliminating the need for multiple processing of the same statement to improve performance.
The sample code is as follows:

 Public voidcomplietest () {using(vardb =NewNorthwindEntities1 ()) {    //compiling the query    varCustomer = Compiledquery.compile<northwindentities1, iqueryable<customers>>(database)= database. Customers.where (c = c.city = ="London")); //perform the same query 20 times     for(inti =0; I < -; i++) {DateTime dt=System.DateTime.Now; foreach(varCinchcustomer (DB))        Console.WriteLine (C.customerid); Console.WriteLine (DateTime.Now.Subtract (DT).        TotalMilliseconds); Console.WriteLine ("---------------------------------------------------"); }     }}

> Storage Model View

In EF, when an entity query is executed, the runtime first transforms the entity model into an ESQL view, and the ESQL view generates the corresponding code based on the MSL file. In addition, the ESQL view contains the corresponding query statements. When the esql view is created, it is cached in the application domain for next use. This runtime generation of a storage model view is a time-consuming process.
In order to eliminate the runtime generation of the storage model view, we can pre-generate this view of the storage model. The steps are as follows:
First, using EDMGEN2 to generate the storage model view, the corresponding commands are as follows:

Edmgen2/viewgen CS NORTHWINDENTITES.EDMX

After executing this command, EDMGEN2 will generate a file named NorthwindEntites.GeneratedViews.cs in the current directory, which is the storage model view file we want to use.
Add this file to your project, and the other code doesn't need to change, and EF calls this view file automatically.
The following example code:

 Public void viewtest () {    using (varnew  NorthwindEntities1 ())    {      var suppliers = db. Suppliers;     foreach (var in suppliers)        Console.WriteLine (s.contactname);    }}

The case where the storage model view is not used is:
1 passed, 0 failed, 0 skipped, took 7.09 seconds.

The NorthwindEntites.GeneratedViews.cs file is added to the project, and the performance is:
1 passed, 0 failed, 0 skipped, took 5.38 seconds.

As you can see, using the storage model view is really improving performance.

> Conflict Management

In EF, concurrency conflicts are not checked by default. Because EF implements optimistic concurrency patterns, the optimistic Concurrency exception exception is thrown when there are concurrent conflicts. We can specify what to do when a conflict occurs by using the RefreshMode enumeration.

The RefreshMode has two enumeration values:
Clientswins: Update the values in the database when committing changes
Storewins: Discard modifications, use values from the database

The sample code snippet is as follows:

varDB2 =NewNorthwindEntities1 (); varCustomer2 = DB2. Customers.firstordefault (c = C.customerid = =" the"); if(Customer2! =NULL) {Customer2. ContactName="Windmill car. Net"; Customer2. City="CD"; Customer2. Region="GX"; }    Try{DB2.    SaveChanges (); }    Catch(Optimisticconcurrencyexception ex)//When a conflict is caught, it is processed accordingly{DB2.    Refresh (Refreshmode.clientwins, Customer2); DB2.    SaveChanges (); }

The code snippet above simply shows how to handle concurrency conflicts, not specific concurrency.

Ways to improve EF code (below)

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.