gpt4 book ai didi

c# - ObjectDisposedException 而实际上不需要连接

转载 作者:行者123 更新时间:2023-11-30 23:18:00 28 4
gpt4 key购买 nike

我收到以下错误:

System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

真的很有趣

1.我使用预加载

2-Tracing 显示加载的值

3-获取数据在代码的第一行有效,然后在后面的几行给出错误

型号:

public class EndedAuctionsModel{
public Auction[] Auctions { get; set; }
public int Count { get; set; }
public int PerPage = 10;
public int Page { get; set; }
}

Controller :

    public ActionResult Ended(EndedAuctionsModel m)
{
m.Page = ((string)ControllerContext.RouteData.Values["id"]).PInt(1);
using(Entities db = new Entities())
{
m.Auctions = db.Auctions.Include("participations").Where(a => a.Ends < DateTime.Now).OrderByDescending(a=>a.Ends).Skip((m.Page-1)*m.PerPage).Take(m.PerPage).ToArray();
m.Count = db.Auctions.Count(a => a.Ends < DateTime.Now);
}
return View(m);
}

查看:

        <div class="participated">
@for (int i = 0; i < Model.Auctions.Length; i++)
{
Participation last = Model.Auctions[i].Participations.OrderBy(p => p.Date).Last();
<div class="col-md-12 auction" style="background:#eee; margin:5px 0;padding:10px;" data-id="@Model.Auctions[i].Id">
<div class="col-md-4"><img src="~/data/pimgs/@Model.Auctions[i].AuctionImages.First().Filename" style="width:100%;border-radius:10px;" /></div>
<div class="col-md-8">
<h3>@Model.Auctions[i].Name</h3>
<div><i class="fa fa-money big-blue"></i>قیمت شروع مزایده: <strong>@Model.Auctions[i].BasePrice.Value.ToString("N0")</strong></div>
@{ string username = last.GetUserName(); if (username == null) { username = "-"; } }
<div><i class="fa fa-gavel big-blue"></i>برنده مزائده: <strong class="last-user"><a href="/Home/Profiles/@username">@username</a></strong></div>
<div><i class="fa fa-user big-blue"></i>قیمت نهایی: <strong class="last-price">@((Model.Auctions[i].BasePrice + Model.Auctions[i].Participations.Count * Model.Auctions[i].StepPrice).Value.ToString("N0"))</strong></div>
</div>
</div>
}
</div>

错误在 View 的第 5 行 data-id="@Model.Auctions[i].Id"

enter image description here

最佳答案

您的 View 包括(在 <img> 标记中)

@Model.Auctions[i].AuctionImages.First().Filename

但您不包括 AuctionImages在您的查询中。改成

m.Auctions = db.Auctions.Include("participations").Include("AuctionImages").Where(....

关于c# - ObjectDisposedException 而实际上不需要连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41111500/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com