簡體   English   中英

ASP.NET Core MVC - System.ObjectDisposedException

[英]ASP.NET Core MVC - System.ObjectDisposedException

這是錯誤的描述:

System.ObjectDisposedException: '無法訪問已釋放的上下文實例。 此錯誤的一個常見原因是釋放從依賴注入中解析的上下文實例,然后嘗試在應用程序的其他地方使用相同的上下文實例。 如果您在上下文實例上調用“Dispose”或將其包裝在 using 語句中,則可能會發生這種情況。 如果你使用依賴注入,你應該讓依賴注入容器負責處理上下文實例。 對象名稱:“AppDbContext”。

我正在嘗試將數據庫記錄設置為我在控制器文件中創建的兩個對象,並將其用於生成 Excel 報告。 但是當我點擊生成報告按鈕時,它給出了這個錯誤。

這是控制器方法的代碼:

        private readonly AppDbContext _db;
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private static IEnumerable<Feedback> sheetfeedbacks;

 public AdminController(AppDbContext db, UserManager<ApplicationUser> userManager, IWebHostEnvironment webHostEnvironment)
        {
           _db = db;
           _userManager = userManager;
            _webHostEnvironment = webHostEnvironment;
        }


public IActionResult FeedbackMgmt()
{
    ParentForApply ParentVM = new ParentForApply();
    var records = _db.FeedbackData;
    ParentVM.FeedbackList = records;           
    sheetfeedbacks = records;
    return View(ParentVM);
}

public IActionResult ReportFeedback()
{           
    using (var workbook = new XLWorkbook())
    {
        var worksheet = workbook.Worksheets.Add("Feedback Data");
        var CurrentRow = 1;
        #region Header
        worksheet.Cell(CurrentRow, 1).Value = "Sr. No";
        worksheet.Cell(CurrentRow, 2).Value = "Full Name";
        worksheet.Cell(CurrentRow, 3).Value = "Suggestions";
        worksheet.Cell(CurrentRow, 4).Value = "Contact No";
        worksheet.Cell(CurrentRow, 5).Value = "Email Id";
        worksheet.Cell(CurrentRow, 6).Value = "City";
        worksheet.Cell(CurrentRow, 7).Value = "State";
        

        #endregion

        #region  Body
        foreach (var fback in sheetfeedbacks)
        {
            CurrentRow++;
            worksheet.Cell(CurrentRow, 1).Value = CurrentRow - 1;
            worksheet.Cell(CurrentRow, 2).Value = fback.FullName;
            worksheet.Cell(CurrentRow, 3).Value = fback.Suggestions;
            worksheet.Cell(CurrentRow, 4).Value = fback.ContactNo;
            worksheet.Cell(CurrentRow, 5).Value = fback.EmailId;
            worksheet.Cell(CurrentRow, 6).Value = fback.City;
            worksheet.Cell(CurrentRow, 7).Value = fback.State;        
            
        }
        

        #endregion

        using (var stream = new MemoryStream())
        {
            workbook.SaveAs(stream);
            var content = stream.ToArray();

            return File(
                content,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "Feedback Data.xlsx"
                );
        }
    }
}

上面提到的錯誤在ReportFeedback()方法中彈出。

您在控制器中使用AppDbContext的唯一地方是這一行

var records = _db.FeedbackData;

只要您想訪問數據庫,就需要使用Task & Await ,我懷疑這是導致錯誤的主要原因

public async Task<IActionResult> FeedbackMgmt()
{
    ParentForApply ParentVM = new ParentForApply();
    var records = await _db.FeedbackData.ToListAsync();
    ParentVM.FeedbackList = records;           
    sheetfeedbacks = records;
    return View(ParentVM);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM