简体   繁体   English

具有自我跟踪实体的c#控制台应用程序:我应该将EF5 DbContext存储在HttpContext中的何处?

[英]c# Console App with self-tracking entities: Where shall I store the EF5 DbContext instead in the HttpContext?

I have a large n-layered web project with self-tracking entities. 我有一个带有自跟踪实体的大型n层Web项目。 I am using Entity Framework 5 and store the object context in HttpContext.Current.Items and dispose it on Application_EndRequest , as described here . 我使用实体框架5和存储在对象上下文HttpContext.Current.Items和处置它Application_EndRequest ,描述在这里 I have a separate Class Library Project for the framework (CMS.Framework) which contains all the (self-tracking) POCO classes and much more logic. 我有一个单独的框架类库项目(CMS.Framework),其中包含所有(自我跟踪)POCO类和更多逻辑。 The actual ASP.NET Web Forms Applications refer to my CMS.Framework project. 实际的ASP.NET Web窗体应用程序引用了我的CMS.Framework项目。 So far, it all works fine. 到目前为止,一切正常。

Now, I want to create a console application (for a scheduled server task) and also use my CMS.Framework class library. 现在,我想创建一个控制台应用程序(用于计划的服务器任务),并且还使用我的CMS.Framework类库。 However, when a self-tracking entity tries to initalize (and calls the static CoreContext property), a System.NullReferenceException is thrown, as HttpContext.Current is null. 但是,当自跟踪实体尝试CoreContext (并调用静态CoreContext属性)时,将抛出System.NullReferenceException ,因为HttpContext.Current为null。 This makes sense to me, as we are in a console app right now. 这对我来说很有意义,就像我们现在在控制台应用程序中一样。 Here is my code that breaks only in the console app: 这是我的代码,仅在控制台应用程序中中断:

    /// <summary>
    /// Gets the CmsCoreContext in order to access the CMS_Core DB. This context
    /// will be disposed when the application onloads (EndRequest).
    /// </summary>
    protected static CmsCoreContext CoreContext
    {
        get
        {
            if (!HttpContext.Current.Items.Contains("CoreContext"))
            {
                // Set Entity Framework context here and reuse it throughout the application
                HttpContext.Current.Items.Add("CoreContext", new CmsCoreContext());
            }
            return HttpContext.Current.Items["CoreContext"] as CmsCoreContext;
        }
    }

Where shall I store the new CmsCoreContext() when HttpContext.Current is null? 当HttpContext.Current为null时,应将new CmsCoreContext()存储在哪里? Is there any console application context I can use? 我可以使用任何控制台应用程序上下文吗? Any tips on this? 有什么提示吗?

Presumably you only want a single instance of your object context for the entire lifetime of your console application? 大概在控制台应用程序的整个生命周期中,您只希望对象上下文的一个实例? Something like this should work: 这样的事情应该起作用:

private static CmsCoreContext _coreContext;

protected static CmsCoreContext CoreContext
{
   get
   {
      if (!System.Web.Hosting.HostingEnvironment.IsHosted)
      {
         return _coreContext ?? (_coreContext = new CmsCoreContext());
      }

      var context = HttpContext.Current;
      if (context == null) throw new InvalidOperationException();

      if (!context.Items.Contains("CoreContext"))
      {
         context.Items.Add("CoreContext", new CmsCoreContext());
      }

      return (CmsCoreContext)context.Items["CoreContext"];
   }
}

// The Application_EndRequest event won't fire in a console application.
public static void ConsoleCleanup()
{
   if (_coreContext != null)
   {
      _coreContext.Dispose();
   }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM