13

While using .NET 3.5 SP1 in ASP.NET MVC application, the ObjectContext can have lifetime on one Http Request OR of a SINGLE method.

using (MyEntities context =  new MyEntities ())
{
//DO query etc
}

How much is increased performance cost of creating ObjectContext in every method VS per request ?

Thanks.

1
  • Good question. Some people mention generating views to increase performance while creating context, but I have no idea if the performance hit is while the app is first run or during every single creation of ObjectContext. Commented Nov 4, 2009 at 2:16

3 Answers 3

17

The cost of creating the context is very low. However, using a new context means that you don't have any cached queries from previous contexts. You can work around this to some degree with view generation or CompiledQuery. See also Performance Considerations for Entity Framework Applications

On the other hand, keeping a context around for a long time means you are tracking increasing amounts of state information, which has a performance cost of its own.

In my opinion, however, the most significant cost of a context is code complication. Using multiple contexts tends to lead to confusing code. So I try to use one context per group of related operations, e.g. handling a single HTTP request.

Sign up to request clarification or add additional context in comments.

2 Comments

"The query plan cache is shared across ObjectContext instances within the same AppDomain. You don't need to hold onto an ObjectContext instance to benefit from query plan caching." [msdn.microsoft.com/en-us/library/…
Yes, the considerations have changed since I wrote this answer 8.5 years ago.
2

I am using EF6 and schema of 163 entities that are db first generated from oracle.

I am measuring Initialization times and time to get 100 records from indexed table.

C# Test

var times = new List<Tuple<DateTime, DateTime, DateTime>>();
var carTypes = new List<CAR_TYPE>();
var j = 1;
while (j <= 10000)
{
    for (int i = 0; i < j; i++)
    {
        var startTime = DateTime.Now;

        using (var db = new EcomEntities())
        {
            var contextInitializationTime = DateTime.Now;
            carTypes = db.CAR_TYPE.Take(100).ToList();
            var executionTime = DateTime.Now;
            times.Add(new Tuple<DateTime, DateTime, DateTime>(startTime, contextInitializationTime, executionTime));
        }
    }
    var averageInitTime = times.Average(o => o.Item2.Subtract(o.Item1).TotalMilliseconds);
    var averageRunTime = times.Average(o => o.Item3.Subtract(o.Item1).TotalMilliseconds);
    Debug.WriteLine("averageInitTime - " + j + " " + averageInitTime);
    Debug.WriteLine("averageRunTime - " + j + " " + averageRunTime);
    j = j*10;
}

Results:

                    Runs        MS                         Runs      MS
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 1     | 134.0134 | averageRunTime  | 1     | 1719.172 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 10    | 12.27395 | averageRunTime  | 10    | 160.3797 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 100   | 1.540695 | averageRunTime  | 100   | 19.94794 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 1000  | 0.281756 | averageRunTime  | 1000  | 6.121224 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 10000 | 0.167058 | averageRunTime  | 10000 | 4.751353 |
+------------------+-------+----------+-----------------+-------+----------+

Comments

1

Is the underlying model small or large, simple or complex? The cost of initializing and using a new objectcontext grows with the size and complexity of the model. If you have a handful of entities, it is usually neglectable. If you have hundreds of entities then it can be significant.

See:
http://oakleafblog.blogspot.com/2008/08/entity-framework-instantiation-times.html
and
http://blogs.msdn.com/adonet/archive/2008/06/20/how-to-use-a-t4-template-for-view-generation.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.