When should I create a new DbContext()

I use a base controller that exposes a DataBase property that derived controllers can access. public abstract class BaseController : Controller { public BaseController() { Database = new DatabaseContext(); } protected DatabaseContext Database { get; set; } protected override void Dispose(bool disposing) { Database.Dispose(); base.Dispose(disposing); } } All of the controllers in my application derive … Read more

Async PartialView causes “HttpServerUtility.Execute blocked…” exception

Child actions must be invoked synchronously. Issue 601 I am also not aware of any recent updates to the current MVC libraries allowing this functionality. A comment on Issue 601, hints at this functionality being added in MVC vNext, aka. MVC6. Child actions look to be replaced with ViewComponent which can be invoked asynchronously from … Read more

Multi-async in Entity Framework 6?

The exception explains clearly that there is only one asynchronous operation per context allowed at a time. So, you either have to await them one at a time as the error message suggests: var banner = await context.Banners.ToListAsync(); var newsGroup = await context.NewsGroups.ToListAsync(); Or you can use multiple contexts: var banner = context1.Banners.ToListAsync(); var newsGroup … Read more

Entity Framework 6 transaction rollback

You don’t need to call Rollback manually because you are using the using statement. DbContextTransaction.Dispose method will be called in the end of the using block. And it will automatically rollback the transaction if the transaction is not successfully committed (not called or encountered exceptions). Following is the source code of SqlInternalTransaction.Dispose method (DbContextTransaction.Dispose will … Read more

The model backing the ‘ApplicationDbContext’ context has changed since the database was created

Just in case anyone else stumbles upon this that was doing a database first implementation like me. I made a change by extending the ApplicationUser class, adding a new field to the AspNetUsers table, and then had this error on startup. I was able to resolve this by deleting the record created in the __MigrationHistory … Read more