locked
Entity Framework - Taking more than a second to return one record. RRS feed

  • Question

  • User-1694925868 posted

    I am using entity framework for the first time on a new application.  The application consists of both web sites and web services.  I noticed while I was testing, Entity Framework was taking over a second for the first time it was reading from a collection.  I'm using an sql connection.  

    Now, I'm not that concerned about performance on the web page.  The whole page only takes 2 seconds to load.  However, the web services I have need to be quite fast.  The web services need to be able to handle 1000 calls per minute.  --- literally.  This is because our customers will be sending information to us in an automated fashion and the information will include lots of data.  Each web service call will call the "SaveChanges" entity framework function once and only once.  Hence, all the second calls being fast means nothing if the first is slow.  One second seems outrageous so I figure I have to be doing something wrong.  

    I've tried setting various configuration settings as both true and false but so far nothing has gotten the performance better than a second.  Here is my code.  Please, tell me what I'm doing wrong.  I may have to give up on entity framework and go to basic ado.net.

    public override DataTable ReturnItem(int p_itemID)
    {
    Entity.Data.DataSets.CustomerSetDataTable dataTable = new Entity.Data.DataSets.CustomerSetDataTable();

    WorldTransferServiceEntities worldTransferService = new WorldTransferServiceEntities();
    //worldTransferService.Configuration.AutoDetectChangesEnabled = false;
    worldTransferService.Configuration.ProxyCreationEnabled = true;
    worldTransferService.Configuration.AutoDetectChangesEnabled = true;
    worldTransferService.Configuration.LazyLoadingEnabled = true;
    worldTransferService.Configuration.ValidateOnSaveEnabled = true;
    System.Data.Common.DbConnection connection = worldTransferService.Database.Connection;
    ConnectionState state = connection.State;
    using (connection)
    {
    if (state != ConnectionState.Open)
    {
    connection.Open();
    }

    var result = worldTransferService.Customers.AsNoTracking().SingleOrDefault(b => b.CustomerID == p_itemID);
    Entity.Data.DataSets.CustomerSetRow row = dataTable.NewCustomerSetRow();
    if (result != null)
    {
    row.CustomerName = result.CustomerName;
    row.SystemApplicationType = result.SystemApplicationType;
    row.VerticalPartnerType = result.VerticalPartnerType;
    dataTable.AddCustomerSetRow(row);
    }
    //connection.Close();
    //connection.Dispose();
    }
    //GC.Collect();
    worldTransferService.Configuration.AutoDetectChangesEnabled = true;
    return dataTable;
    }

    Wednesday, May 6, 2015 12:30 PM

Answers

  • User61956409 posted

    Hi Ben,

    Thanks for your post.

    The very first time any query is made against a given model, the Entity Framework does a lot of work behind the scenes to load and validate the model. We frequently refer to this first query as a "cold" query.  Further queries against an already loaded model are known as "warm" queries, and are much faster.  

    This article shows the performance considerations when developing applications using the Entity Framework, please refer to it.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2015 2:27 AM