在我的控制器中,如何执行我的 table PageContent 的每个操作并仅更新每条记录内的字段
In my controller , How to do foreach of my table PageContent and update only to field inside of each record the records
我想更新控制器中 table PageContent 的所有记录。
如何执行 foreach 或 for 以更新我 table 的所有记录,但我只想更新两个字段:
fetched.PageContentVisible = false;
fetched.PageContentPhoto = "Submenu" + "-" + item.PageContentId + "-" + item.MenuSubId + "-" + item.PageSectionId;
这是代码:
// GET: PageContents/UpdateScript
public async Task<IActionResult> UpdateScript()
{
using (var transaction = new TransactionScope())
{
var pageContent = _context.PageContent.Include(p => p.ParentMenuPage).Include(p => p.ParentPageSection);
using (_context)
{
foreach (var item in pageContent)
{
var fetched = _context.PageContent.Find(item.PageContentId);
fetched.PageContentPhoto = "PageContent" + "-" + item.PageContentId + "-" + item.MenuSubId + "-" + item.PageSectionId;
if (item.PageSectionId == 2)
{
fetched.PageContentVisible = false;
}
_context.Entry(fetched).State = EntityState.Modified;
//await _context.SaveChangesAsync();
_context.SaveChanges();
}
}
transaction.Complete();
return View(await pageContent.ToListAsync());
}
}
但由于这个错误它不起作用:
ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ApplicationDbContext'.
Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
CateringMilano.Controllers.PageContentsController.UpdateScript() in PageContentsController.cs
+
return View(await pageContent.ToListAsync());
感谢您的建议
根据您的评论,您可以尝试以下代码片段:
public async Task<IActionResult> UpdateScript()
{
using (var transaction = new TransactionScope())
{
var pageContent = _context.PageContent.Include(p => p.ParentMenuPage).Include(p => p.ParentPageSection);
using (_context)
{
foreach (var item in pageContent)
{
var fetched = _context.PageContent.Find(item.PageContentId);
fetched.PageContentPhoto = "PageContent" + "-" + item.PageContentId + "-" + item.MenuSubId + "-" + item.PageSectionId;
if (item.PageSectionId == 2)
{
fetched.PageContentVisible = false;
}
_context.Entry(fetched).State = EntityState.Modified;
//await _context.SaveChangesAsync();
_context.SaveChanges();
}
}
transaction.Complete(); // Here already your transaction completed,so this line will not work await pageContent.ToListAsync()
return RedirectToAction("GetUpdatedPageContent"); // Redirect to new View and there you can read the new data and return the view like below
}
}
Note:
On the new view read your new updated data and return this value to
view. which will resolve your problem.See the example below.
public async Task<IActionResult> UpdateScript()
{
var pageContent = _context.PageContent.Include(p => p.ParentMenuPage).Include(p => p.ParentPageSection);
return View(pageContent); // Redirect this view finally
}
Reason of error::
As you have completed your scope
here transaction.Complete()
so
after that you cannot do anything because db connection
already closed for this scope.
So you cannot access this await pageContent.ToListAsync()
any more.
This is why you are getting this error.
希望以上步骤能帮助您相应地解决问题。
我想更新控制器中 table PageContent 的所有记录。 如何执行 foreach 或 for 以更新我 table 的所有记录,但我只想更新两个字段:
fetched.PageContentVisible = false;
fetched.PageContentPhoto = "Submenu" + "-" + item.PageContentId + "-" + item.MenuSubId + "-" + item.PageSectionId;
这是代码:
// GET: PageContents/UpdateScript
public async Task<IActionResult> UpdateScript()
{
using (var transaction = new TransactionScope())
{
var pageContent = _context.PageContent.Include(p => p.ParentMenuPage).Include(p => p.ParentPageSection);
using (_context)
{
foreach (var item in pageContent)
{
var fetched = _context.PageContent.Find(item.PageContentId);
fetched.PageContentPhoto = "PageContent" + "-" + item.PageContentId + "-" + item.MenuSubId + "-" + item.PageSectionId;
if (item.PageSectionId == 2)
{
fetched.PageContentVisible = false;
}
_context.Entry(fetched).State = EntityState.Modified;
//await _context.SaveChangesAsync();
_context.SaveChanges();
}
}
transaction.Complete();
return View(await pageContent.ToListAsync());
}
}
但由于这个错误它不起作用:
ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ApplicationDbContext'. Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
CateringMilano.Controllers.PageContentsController.UpdateScript() in PageContentsController.cs + return View(await pageContent.ToListAsync());
感谢您的建议
根据您的评论,您可以尝试以下代码片段:
public async Task<IActionResult> UpdateScript()
{
using (var transaction = new TransactionScope())
{
var pageContent = _context.PageContent.Include(p => p.ParentMenuPage).Include(p => p.ParentPageSection);
using (_context)
{
foreach (var item in pageContent)
{
var fetched = _context.PageContent.Find(item.PageContentId);
fetched.PageContentPhoto = "PageContent" + "-" + item.PageContentId + "-" + item.MenuSubId + "-" + item.PageSectionId;
if (item.PageSectionId == 2)
{
fetched.PageContentVisible = false;
}
_context.Entry(fetched).State = EntityState.Modified;
//await _context.SaveChangesAsync();
_context.SaveChanges();
}
}
transaction.Complete(); // Here already your transaction completed,so this line will not work await pageContent.ToListAsync()
return RedirectToAction("GetUpdatedPageContent"); // Redirect to new View and there you can read the new data and return the view like below
}
}
Note:
On the new view read your new updated data and return this value to view. which will resolve your problem.See the example below.
public async Task<IActionResult> UpdateScript()
{
var pageContent = _context.PageContent.Include(p => p.ParentMenuPage).Include(p => p.ParentPageSection);
return View(pageContent); // Redirect this view finally
}
Reason of error::
As you have completed your
scope
heretransaction.Complete()
so after that you cannot do anything becausedb connection
already closed for this scope. So you cannot access thisawait pageContent.ToListAsync()
any more. This is why you are getting this error.
希望以上步骤能帮助您相应地解决问题。