"Entity Framework Data insertion Performance Tracking" post-read summary

Source: Internet
Author: User

Park friend Leibniz wrote a "Entity Framework data Insert Performance Tracking" article, I feel good, at least he raised the question, wrote out, aroused everyone's discussion, this is an atmosphere. After reading the article + comments, so I also wrote a simple program to try.

First, the code:

Two simple classes:

   1://      <summary>
   2://      consumer
   3://      </summary>
   4:      class Consumer
   5:      {
   6:          int CId {get; set;}
   7:          string CName {get; set;}
   8: Public          list<order> Orders {get; set;}
   9:      }
  Ten:  
  One://      <summary>
  ///      order
  ///      </summary>
  :      class Order
  :      {
  :          int OrderNo {get; set;}
  N: Public          DateTime OrderDate {get; set;}
  :          decimal Totalmoney {get; set;}
  :          int CId {get; set;}
  :  
  : Public          Consumer Consumer {get; set;}
  :      }

Mapping configuration:

   1:      class consumerconfiguration:entitytypeconfiguration<consumer>
   2:      {
   3: Public          consumerconfiguration ()
   4:          {
   5:              haskey (t = t.cid). Property (t = t.cid). Hasdatabasegeneratedoption (databasegeneratedoption.identity);
   6: Property              (t = = t.cname). IsRequired (). Hasmaxlength (50);
   7:          }
   8:      }
   9:  
  Ten:      class orderconfiguration:entitytypeconfiguration<order>
  One:      {
  : Public          orderconfiguration ()
  :          {
  :              haskey (t = t.orderno);
  :              hasrequired (t = t.consumer). Withmany (t = t.orders). Hasforeignkey (t = t.cid);
  :          }
  :      }

Context:

   1:      class Testcontext:dbcontext
   2:      {
   3: Public          dbset<consumer> consumers {get; set;}
   4: Public          dbset<order> Orders {get; set;}
   5:  
   6:          void onmodelcreating (Dbmodelbuilder modelBuilder)
   7:          {
   8:              modelbuilder.conventions.remove<pluralizingtablenameconvention> ();
   9:  
  Ten:              modelBuilder.Configurations.Add (new Consumerconfiguration ());
  One:              modelBuilder.Configurations.Add (new Orderconfiguration ());
  :  
  :              base. Onmodelcreating (ModelBuilder);
  :          }
  :      }

Test code:

   1:          void Main (string[] args) 
   2:          {
   3:              New TestContext ())
   4:              {
   5:                  CTX. Consumers.add (new Consumer ()
   6:                  {
   7:                      "Zhang San"
   8:                  });
   9:                  CTX. SaveChanges ();
  Ten:  
  One:                  new Stopwatch ();
  :                  SW. Start ();
  :                  Console.WriteLine ("Order start: \ n");
  :  
  : For                  (int outer = 0; outer < 20000; outer++) 
  :                  {
  :                      CTX. Orders.add (new Order ()
  :                            {
  :                                OrderDate = DateTime.Now,
  :                                Totalmoney = 100,
  :                                CId = 1,
  :                            });
  :                  }
  :                  CTX. SaveChanges ();
  :                  SW. Stop ();
  :                  "milliseconds");
  :              }
  :          }

The code above is the most common code, there is nothing to explain, put the content on the focus.

The environment running the above code is Vs2012+sql SERVER R2, machine configuration: 4g,n years ago CPU.

Running the above code is very slow , as Leibniz said, and it is time-consuming to add data to the context. This problem occurs because of each call to CTX. Before Orders.add (order), EF will call Detectchanges, which is explained on StackOverflow, and the address is: http://stackoverflow.com/questions/9439430/ Improving-performance-of-initializing-dbset-in-seed, in addition to the programming Entity Framework DbContext 60 of the book also has a Detectchange introduction.

The solution to the problem of slow speed above is to set

   1:  false;

Let's look at the following to disable the subsequent execution speed:

Another solution is to use dbset<t>. AddRange method, this method is added in the 6.0 Beta1.

   1:                  new list<order> ();
   2: For                  (int outer = 0; outer < 20000; outer++) 
   3:                  {
   4:                      orderlist.add (new Order)
   5:                             {
   6:                                 OrderDate = DateTime.Now,
   7:                                 Totalmoney = 100,
   8:                                 CId = 1
   9:                             });
  Ten:                  }
  One:                  ctx. Orders.addrange (orderlist);
  :                  CTX. SaveChanges ();

The AddRange method in the System.Data.Entity generic Dbset class is the graph that I cut through the reflector

As you can see from the two images above, both add and AddRange are added to _internalset, but if autodetectchangesenabled is set to True, Detectchanges will be called before any entities are added. Watch the explanations in remarks.

Test Source: http://www.ef-community.com/forum.php?mod=viewthread&tid=437&extra=page%3D1

"Entity Framework Data insertion Performance Tracking" post-read summary

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.