Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating one ObjectContext instance per HTTP request - EF & ASP.NET

Status
Not open for further replies.

jondow

Programmer
Oct 19, 2006
45
GB
VS 2010, .NET 4.0, C#

Hello All,

I’m currently planning a new web app and intend to use entity framework for the first time. I have read a few articles that say that one ObjectContext instance should be created per HTTP request and I’m trying to find out how this can be implemented. I have seen a few very complex examples but I’m finding them hard to follow. Basically what I want to do is select a product which I will display in a user control, then at a later point in the request lifecycle I will need to get a list of categories, related items etc.

Product selection:
using (acmsEntities oEntities = new acmsEntities())
{
ObjectQuery<acms_Product> oProduct = oEntities.acms_Product;
IQueryable<acms_Product> query = from p in oProduct where p.pro_id == ProductID select p;

EntityProduct = query.FirstOrDefault();
}

Could anyone tell me the best way to implement this (ideally with examples) and also explain the mechanisms involved in maintaining the object context for the life of the request?
Does the entity framework have this functionality built-in or does it require custom objects to persist the object context?
How intensive is the creation of each object context instance and is there a big resource overhead?


Thanks for your help!

Rick
 
check out faq855-7190

In this FAQ I describe how to manage the database connection with the ADO.Net objects for an asp.net request. The same concepts apply to the EF DataContext. Instead of an IDbConnection you will have a DataContext. Instead of IDbCommands and IDbParameters you will have linq queries.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for this Jason, I'll have a good read through later.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top