title: ASP.NET Core database configuration with dependency injection issue
body: <p>I ran in to a problem 6 days ago and i still can't figure it out.</p>
  
  <p>I'm setting up a DBContext using DI:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      // Add framework services.
      services.AddDbContext&lt;ApplicationContext&gt;(options =&gt;
          options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
  
      services.AddMvc();
  
      // Add application services.
      services.AddTransient&lt;IEmailSender, AuthMessageSender&gt;();
      services.AddTransient&lt;ISmsSender, AuthMessageSender&gt;();
  }
  </code></pre>
  
  <p>The database file path is configurered in the appsettings.json</p>
  
  <pre><code>{
    "ConnectionStrings": {
      "DefaultConnection": "Data Source=xxxx.db"
    },
    "Logging": {
      "IncludeScopes": false,
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
  }
  </code></pre>
  
  <p>And I'm creating a DBContext like this (<a href="https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext</a>)</p>
  
  <pre><code>public class ApplicationContext : DbContext
  {
          public DbSet&lt;Network&gt; xxxx { get; set; }
          public DbSet&lt;NetworkEntry&gt; xxxx { get; set; }
          public ApplicationContext(DbContextOptions&lt;ApplicationContext&gt; options)
          : base(options)
          { }
  }
  </code></pre>
  
  <p>My controller code:</p>
  
  <pre><code>public JsonResult Index(ApplicationContext db)
  {
      return Json(db.Networks);
  }
  </code></pre>
  
  <p>It compiles and runs. If I use the other way of configuring the DB, it works 100% problem free:</p>
  
  <pre><code>protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
      optionsBuilder.UseSqlite("Filename=./blog.db");
  }
  </code></pre>
  
  <p>The problem I'm having:</p>
  
  <pre><code>System.InvalidOperationException: Could not create a model binder for model object of type 'xxx.Data.ApplicationContext'.
     at Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory.CreateBinder(ModelBinderFactoryContext context)
     at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.&lt;BindModelAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.&lt;BindArgumentsCoreAsync&gt;d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.&lt;InvokeNextResourceFilter&gt;d__22.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.&lt;InvokeAsync&gt;d__20.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Builder.RouterMiddleware.&lt;Invoke&gt;d__4.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.&lt;Invoke&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.&lt;Invoke&gt;d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.&lt;Invoke&gt;d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.&lt;Invoke&gt;d__7.MoveNext()
  </code></pre>
  
  <p>Any clues?</p>