title: How to boost Entity Framework Unit Of Work Performance
body: <p>Please see the following situation:</p>
  
  <p>I do have a CSV files of which I import a couple of fields (not all in SQL server using Entity Framework with the Unit Of Work and Repository Design Pattern).</p>
  
  <pre><code>var newGenericArticle = new GenericArticle
  {
      GlnCode = data[2],
      Description = data[5],
      VendorId = data[4],
      ItemNumber = data[1],
      ItemUOM = data[3],
      VendorName = data[12]
  };
  
  var unitOfWork = new UnitOfWork(new AppServerContext());
  unitOfWork.GenericArticlesRepository.Insert(newGenericArticle);
  
  unitOfWork.Commit();
  </code></pre>
  
  <p>Now, the only way to uniquely identify a record, is checking on 4 fields: <code>GlnCode</code>, <code>Description</code>, <code>VendorID</code> and <code>Item Number</code>.</p>
  
  <p>So, before I can insert a record, I need to check whether or not is exists:</p>
  
  <pre><code> var unitOfWork = new UnitOfWork(new AppServerContext());
  
   // If the article is already existing, update the vendor name.
   if (unitOfWork.GenericArticlesRepository.GetAllByFilter(
           x =&gt; x.GlnCode.Equals(newGenericArticle.GlnCode) &amp;&amp;
                x.Description.Equals(newGenericArticle.Description) &amp;&amp;
                x.VendorId.Equals(newGenericArticle.VendorId) &amp;&amp;
                x.ItemNumber.Equals(newGenericArticle.ItemNumber)).Any())
   {
       var foundArticle = unitOfWork.GenericArticlesRepository.GetByFilter(
           x =&gt; x.GlnCode.Equals(newGenericArticle.GlnCode) &amp;&amp;
                x.Description.Equals(newGenericArticle.Description) &amp;&amp;
                x.VendorId.Equals(newGenericArticle.VendorId) &amp;&amp;
                x.ItemNumber.Equals(newGenericArticle.ItemNumber));
  
       foundArticle.VendorName = newGenericArticle.VendorName;
  
       unitOfWork.GenericArticlesRepository.Update(foundArticle);
   }
  </code></pre>
  
  <p>If it's existing, I need to update it, which you see in the code above.</p>
  
  <p>Now, you need to know that I'm importing around 1.500.000 records, so quite a lot.
  And it's the filter which causes the CPU to reach almost 100%.</p>
  
  <p>The `GetAllByFilter' method is quite simple and does the following:</p>
  
  <pre><code>return !Entities.Any() ? null : !Entities.Where(predicate).Any() ? null : Entities.Where(predicate).AsQueryable();
  </code></pre>
  
  <p>Where <code>predicate</code> equals <code>Expression&lt;Func&lt;TEntity, bool&gt;&gt;</code></p>
  
  <p>Is there anything that I can do to make sure that the server's CPU doesn't reach 100%?</p>
  
  <p>Note: I'm using SQL Server 2012</p>
  
  <p>Kind regards</p>