Cannot access a disposed object in ASP.NET Core when injecting DbContext

Find the root cause of what really happened to you disposable object.

Working with ASP.NET core I have seen this error multiple times and can be hard to debug. The error can have multiple root causes. It can be a real pain to find the cause and it is sometimes fixed by only taking care of the symptoms. By finding the root cause, you can take the appropriate action in your code.

Continue reading “Cannot access a disposed object in ASP.NET Core when injecting DbContext”

Advertisement

Add Index with Include Entity Framework Core

This post explaines how to add index to EF Core with extra columns included from code.

When creating indexes with code first migrations in Entity Framework Core you can create an index on a table by adding the following to your DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<table>() 
        .HasIndex(t =&gt; new { t.Column1, t.Column2}); } 

Continue reading “Add Index with Include Entity Framework Core”