ASP.NET MVC with Entity Framework

I think it would be wise to configure the context of the Entity object in Application_BeginRequest, save it in Request.items, use it throughout the request and manage it in Application_EndRequest. That way, context is always available, and I can navigate the Entity Framework object graph in my views, lazy loading what I did not want.

I think that would make it work on Ruby on Rails .

Perhaps I would be shot for such a heresy, but it was so simple :(

I cannot get Application_BeginRequest and..EndRequest to start ASP.NET MVC . Aren't they fired? Any special trick I need to do?

+6
asp.net-mvc entity-framework
source share
2 answers

1.0 build ASP.NET MVC allows me to attach event handlers to both beginrequest and endrequest, update SessionScope and store it in HttpContext.Items at the beginning of the request (I switched to Castle ActiveRecord), and in the end I select the session one from HttpContext. Items and delete it. This provides lazy loading throughout the entire request life cycle. (It can even navigate the graph of objects in views.)

+5
source share

An object context in EF, such as a data context in L2S, is designed as a "unit of work", they are not thread safe, and they are not intended for long-term existence.

In MVC, the best strategy is to create it in the controller’s constructor (implicitly or explicitly, it doesn’t matter) and then discard it in the Dispose method. Of course, EF doesn't do lazy loading, so you have to find your own way to be lazy. :)

+5
source share

All Articles