0

I am tring to update the field PageContentPhoto of my Table PageContent using this code:

var applicationDbContext = _context.PageContent.Include(p => p.ParentMenuPage).Include(p => p.ParentPageSection);
var chooseMyscript = "script1";
if (chooseMyscript == "script1")
{
    foreach (var item in applicationDbContext)
    {
        var fetched = _context.PageContent.Find(item.PageContentId);
        fetched.PageContentPhoto = "Submenu" + "-" + item.PageContentId + "-" + item.MenuSubId + "-" + item.PageSectionId;
        if (item.PageSectionId == 2)
        {
            fetched.PageContentVisible = false;
        }
        _context.Entry(fetched).State = EntityState.Modified;
        _context.SaveChanges();
    }
}

return View(await applicationDbContext.ToListAsync());

When i run it I get this error:

An unhandled exception occurred while processing the request. SqlException: New transaction is not allowed because there are other threads running in the session. Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, bool breakConnection, Action wrapCloseInAction)

What is wrong?

Serge
  • 28,094
  • 4
  • 11
  • 35
  • I already checked the others answers... and none of them resolve my problem, i guess i have to create another db context... but i do not know how to do.. can you help please. – Tamil Cantroz Feb 14 '22 at 14:43

0 Answers0