C# 存储ObjectContext实例的最佳位置是哪里?

C# 存储ObjectContext实例的最佳位置是哪里?,c#,asp.net-mvc,linq-to-sql,entity-framework,C#,Asp.net Mvc,Linq To Sql,Entity Framework,我使用对象扩展名,因为它提供非常短的可访问字符串 public static BusinessLayer.Models.SearchEngineEntities db(this object o) { if (HttpContext.Current == null) return new BusinessLayer.Models.SearchEngineEntities(ConfigurationManager.ConnectionStrings

我使用对象扩展名,因为它提供非常短的可访问字符串

public static BusinessLayer.Models.SearchEngineEntities db(this object o)
    {

        if (HttpContext.Current == null)
            return new BusinessLayer.Models.SearchEngineEntities(ConfigurationManager.ConnectionStrings["SearchEngineEntities"].ConnectionString);

        if (HttpContext.Current.Items.Contains("DataContext"))
        {
            return (BusinessLayer.Models.SearchEngineEntities)HttpContext.Current.Items["DataContext"];
        }

        BusinessLayer.Models.SearchEngineEntities context = new BusinessLayer.Models.SearchEngineEntities(ConfigurationManager.ConnectionStrings["SearchEngineEntities"].ConnectionString);
        HttpContext.Current.Items.Add("DataContext", context);
        return context;
    }
因此,在结果中,我有如下执行字符串:

"".db().Users.Include("")....
但我不是100%确定这是最好的解决方案。我希望您比我更了解在何处以及如何存储DataContext或ObjectContext实例。也许你知道更好的解决办法

谢谢您的建议。

为什么需要扩展方法来完成此操作?您应该放弃扩展方法,只使用
静态
方法(和
静态类
)。我还发现了一篇文章,我认为它可能会帮助你: