Reputation: 1321
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.
Upvotes: 13
Views: 5206
Reputation: 61409
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 |
+------------------+-------+----------+-----------------+-------+----------+
Upvotes: 2
Reputation: 12397
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
Upvotes: 1
Reputation: 126547
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.
Upvotes: 17