Entity Framework DateTime update extremely slow

Posted by Phyxion on Stack Overflow See other posts from Stack Overflow or by Phyxion
Published on 2012-06-01T16:36:38Z Indexed on 2012/06/01 16:41 UTC
Read the original article Hit count: 237

I have this situation currently with Entity Framework:

using (TestEntities dataContext = DataContext)
            {
                UserSession session = dataContext.UserSessions.FirstOrDefault(userSession => userSession.Id == SessionId);
                if (session != null)
                {
                    session.LastAvailableDate = DateTime.Now;
                    dataContext.SaveChanges();
                }
            }

This is all working perfect, except for the fact that it is terribly slow compared to what I expect (14 calls per second, tested with 100 iterations). When I update this record manually through this command:

dataContext.Database.ExecuteSqlCommand(String.Format("update UserSession set LastAvailableDate = '{0}' where Id = '{1}'", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff"), SessionId));

I get 55 calls per second, which is more than fast enough. However, when I don't update the session.LastAvailableDate but I update an integer (e.g. session.UserId) or string with Entity Framework, I get 50 calls per second, which is also more than fast enough. Only the datetime field is terrible slow.

The difference of a factor 4 is unacceptable and I was wondering how I can improve this as I don't prefer using direct SQL when I can also use the Entity Framework.

I'm using Entity Framework 4.3.1 (also tried 4.1).

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf