绕过“Dispose 之后访问 DataContext” ASP.NET MVC 视图中的错误

发布于 2024-10-04 11:23:46 字数 1118 浏览 10 评论 0原文

我有一个 ASP.NET MVC 2 操作,如下所示:

public ActionResult Index()
    {
        using(var db = new MyDataContext())
        {
            var welcomeSnippet = "test";
            var articles = db.Posts.Where(p => p.DateOfPublish <= DateTime.Now).Take(5).ToList();
            return View(new HomeViewModel()
            {
                Articles = articles,
                WelcomeSnippet = welcomeSnippet
            });
        }
    }

该视图包含以下代码:

<%foreach (var item in Model.Articles)
{%>
     <div class="article" id="<%=item.PostID %>">
          <!-- some properties -->
          <div class="tags">tags: <i><%foreach (var tag in item.PostTags.ToList())
                                        { %><%=Html.Encode(tag.Tag.TagName.Trim())%> <%} %></i>
          </div>
     </div>
<% } %>

我正在访问 item.PostTags,它是通过我的 DataContext 获取的。在这里,我本质上使用的是延迟加载,但我收到一个错误:当列出这些 PostTags 时,我的 DataContext 已经被释放。

如何在释放 DataContext 之前加载此类数据?

I have an ASP.NET MVC 2 action that looks like this:

public ActionResult Index()
    {
        using(var db = new MyDataContext())
        {
            var welcomeSnippet = "test";
            var articles = db.Posts.Where(p => p.DateOfPublish <= DateTime.Now).Take(5).ToList();
            return View(new HomeViewModel()
            {
                Articles = articles,
                WelcomeSnippet = welcomeSnippet
            });
        }
    }

The view includes the following code:

<%foreach (var item in Model.Articles)
{%>
     <div class="article" id="<%=item.PostID %>">
          <!-- some properties -->
          <div class="tags">tags: <i><%foreach (var tag in item.PostTags.ToList())
                                        { %><%=Html.Encode(tag.Tag.TagName.Trim())%> <%} %></i>
          </div>
     </div>
<% } %>

I'm accessing item.PostTags, which is obtained through my DataContext. Here, I'm essentially using lazy-loading, but I'm getting an error: my DataContext is already disposed when it comes time to list those PostTags.

How can I load such data before my DataContext is disposed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2024-10-11 11:23:46

两个选项:

1)手动处理 DC(例如 Global.asax 中的 Application_EndRequest)

2)在控制器中急切加载 item.Tags

using(var db = new MyDataContext())
        {
            var welcomeSnippet = "test";
            var articles = db.Posts.Include("PostTags").Where(p => p.DateOfPublish <= DateTime.Now).Take(5).ToList();

            return View(new HomeViewModel()
            {
                Articles = articles,
                WelcomeSnippet = welcomeSnippet
            });
        }

我会选择选项 2,因为选项 1 在不使用DI 容器。 (你显然没有使用)。

编辑

抱歉 - 我以为您使用的是实体框架,这里是“渴望加载”的 L2SQL 等效项:

您需要使用 DataLoadOptions

using(var db = new MyDataContext())
        {
            var dataLoadOptions = new DataLoadOptions();
            dataLoadOptions.LoadWith<Post>(x => x.PostTags);
            db.LoadOptions = dataLoadOptions;

            var welcomeSnippet = "test";
            var articles = db.Posts.Where(p => p.DateOfPublish <= DateTime.Now).Take(5).ToList();

            return View(new HomeViewModel()
            {
                Articles = articles,
                WelcomeSnippet = welcomeSnippet
            });
        }

Two options:

1) Manually Dispose of DC (e.g Application_EndRequest in Global.asax)

2) Eager load item.Tags in Controller:

using(var db = new MyDataContext())
        {
            var welcomeSnippet = "test";
            var articles = db.Posts.Include("PostTags").Where(p => p.DateOfPublish <= DateTime.Now).Take(5).ToList();

            return View(new HomeViewModel()
            {
                Articles = articles,
                WelcomeSnippet = welcomeSnippet
            });
        }

I would go option 2, as option 1 is risky without the use of a DI container. (which your obviously not using).

EDIT

Sorry - i thought you were using Entity Framework, here is the L2SQL equivalent of "eager loading":

You need to use DataLoadOptions

using(var db = new MyDataContext())
        {
            var dataLoadOptions = new DataLoadOptions();
            dataLoadOptions.LoadWith<Post>(x => x.PostTags);
            db.LoadOptions = dataLoadOptions;

            var welcomeSnippet = "test";
            var articles = db.Posts.Where(p => p.DateOfPublish <= DateTime.Now).Take(5).ToList();

            return View(new HomeViewModel()
            {
                Articles = articles,
                WelcomeSnippet = welcomeSnippet
            });
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文