简体   繁体   English

我应该在MVC应用程序中的哪个位置创建实体框架上下文对象?

[英]Where should I create my Entity Framework context object in an MVC app?

I would like to use the Session-per-request pattern, as is done frequently when using NHibernate in an ASP.NET environment. 我想使用Session-per-request模式,就像在ASP.NET环境中使用NHibernate时经常做的那样。

My first thought was to put the code to create the context in the BeginRequest event handler in Global.asax, but I discovered that this event is firing not only for the initial request that actually does the work, but also for the subsequent requests for static files such as CSS and images. 我的第一个想法是将代码放在Global.asax中的BeginRequest事件处理程序中创建上下文,但我发现这个事件不仅针对实际执行工作的初始请求,而且针对随后的静态请求CSS和图像等文件。

I don't want to create a whole bunch of extra contexts when they're not needed, so is there a way I can just get my code to run on the initial request, and not the ones for the static files? 我不想在不需要的时候创建一大堆额外的上下文,所以有没有办法让我的代码在初始请求上运行,而不是静态文件的代码?

  1. Use constructor injection for anything which needs a context. 对任何需要上下文的东西使用构造函数注入。
  2. Use the DI framework of your choice and map the context. 使用您选择的DI框架并映射上下文。
  3. Scope the context as request-scoped. 将上下文作为请求范围限定。

If you are running in IIS 7 with an integrated pipeline (the default), your ASP.NET module will see every request, even the requests for static content (see http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/ ), 如果您使用集成管道(默认)在IIS 7中运行,则ASP.NET模块将查看每个请求,甚至是静态内容请求(请参阅http://learn.iis.net/page.aspx/508/ wildcard-script-mapping-and-iis-7-integrated-pipeline / ),

If you still want to use an HTTP module to manage a session/content per request, I'd consider using Lazy to avoid instantiating the context when it is not needed. 如果您仍然希望使用HTTP模块来管理每个请求的会话/内容,我会考虑使用Lazy来避免在不需要时实例化上下文。

If you use an action filter, you'll need to finish all the work early if you dispose the context during OnActionExecuted. 如果您使用动作过滤器,则在OnActionExecuted期间处置上下文时,您需要提前完成所有工作。 Wait and dispose during OnResultExecuted if you want to defer query execution until the view renders, or use deferred / lazy loading of entities. 如果要延迟查询执行直到视图呈现,或者使用延迟/延迟加载实体,请在OnResultExecuted期间等待并释放。

As Craig pointed out, the IoC containers can also manage the lifetime for you. 正如Craig指出的那样,IoC容器也可以为您管理生命周期。

When I'm using a dependency injection framework I typically have a separate assembly with the domain model in it. 当我使用依赖注入框架时,我通常有一个单独的程序集,其中包含域模型。 That assembly also contains interfaces for repositories. 该程序集还包含存储库的接口。 The implementation of the repository contains the code that actually instantiates the EF ObjectContext. 存储库的实现包含实际实例化EF ObjectContext的代码。

If this is a really simple application though and you're not doing DI you could always instantiate the EF ObjectContext within the constructor of your controller. 如果这是一个非常简单的应用程序,并且你没有做DI,你总是可以在控制器的构造函数中实例化EF ObjectContext。 Have a look at the ASP.NET MVC sample applications at http://www.asp.net/mvc for good basic information on getting started. 有关入门的基本信息, 查看http://www.asp.net/mvc上的ASP.NET MVC示例应用程序。 I think all three of the sample applications they have use EF now. 我认为他们现在使用EF的所有三个示例应用程序。

Are you using IIS 6 with wildcard mapping? 您是否使用带有通配符映射的IIS 6? CSS and Images shouldn't trigger BeginRequest. CSS和图像不应该触发BeginRequest。

Another popular way to do this is by overriding the OnActionExecuted and OnActionExecuting methods inside of a base Controller. 另一种流行的方法是覆盖基本Controller内部的OnActionExecuted和OnActionExecuting方法。

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _context = new SomeEntities();
        base.OnActionExecuting(filterContext);
    }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _context.Dispose();
        base.OnActionExecuted(filterContext);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net MVC:我的设计应该在哪里创建/声明实体键? - ASP.net MVC: Where in my design should I create/declare entity keys? MVC 4-实体框架,ViewModel和对象上下文 - MVC 4 - Entity Framework, ViewModel and Object Context 我应该将EF实体和数据注释放在asp.net mvc +实体框架项目中的哪里 - where should I put the EF entity and data annotations in asp.net mvc + entity framework project 我应该映射我的MVC3模型和实体框架模型吗? 怎么样? - Should I map my MVC3 Models and my Entity Framework Models and if; how? ASP.NET MVC中的实体框架和对象上下文生存期 - Entity Framework and Object Context lifetime in ASP.NET MVC 使用实体框架在哪里创建上下文的分层体系结构 - Layered Architecture using Entity framework where to create the context MVC 2和实体框架 - 我应该将Entity归入单独的层吗? - MVC 2 and Entity Framework - Should I put Entity classed in separate layer? 我应该为新的mvc项目使用实体框架还是linq to sql? - Should I use the entity framework or linq to sql for new mvc project? 我应该在哪里创建我的AutoMapper映射? - Where should I create my AutoMapper mappings? 我应该在哪里创建静态方法? - Where should i create my static methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM