Bicycle Dave
Bicycle Dave

Reputation: 484

Asp.Net Core Entity Framework in memory - no provider

I'm trying to follow the sample in the Asp.Net Core documentation for adding a View component. The component uses an in memory data store. But I can't get the in memory working. I don't understand how to comply with the instructions in the error message. What am I missing? This is the error:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

Which happens in this code in the lines for context.add(new ToDoItem):

namespace ViewComponentSample.Models
{
public class SeedData
{
    public static void Initialize(IServiceProvider serviceProvider)
    {
        var context = serviceProvider.GetService<ToDoContext>();

        if (context.Database == null)
        {
            throw new Exception("DB is null");
        }

        for (int i = 0; i < 9; i++)
        {
            context.Add(new TodoItem()
            {
                IsDone = i % 3 == 0,
                Name = "Task " + (i + 1),
                Priority = i % 5 + 1
            });
        }
        context.SaveChanges();
    }

}
}

I have these dependencies in my project.json:

"dependencies": {
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": {
  "version": "1.0.0-preview2-final",
  "type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.InMemory": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"BundlerMinifier.Core": "2.2.281",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.NETCore.App": "1.0.0",
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0",
"Microsoft.Extensions.DependencyInjection": "1.0.0",
"Microsoft.AspNet.Routing": "1.0.0-rc1-final"

},

This is in my Startup.cs ConfigureServices:

  services.AddEntityFrameworkInMemoryDatabase().AddDbContext<ToDoContext>(optionsAction => optionsAction.UseInMemoryDatabase());

This is the Model:

using Microsoft.EntityFrameworkCore;

namespace ViewComponentSample.Models
{
public class ToDoContext : DbContext
{
    public DbSet<TodoItem> ToDo { get; set; }
}
}

Upvotes: 2

Views: 1769

Answers (1)

rudolfdobias
rudolfdobias

Reputation: 1948

When you called in configure time this

...(optionsAction => optionsAction.UseInMemoryDatabase())

you only registered an instance of DbContextOptions<ToDoContext> into DI. But your ToDoContext does not know about that.

The exception says that you have to inject the configuration you created by into the ToDoContext.

You have to do this:

using Microsoft.EntityFrameworkCore;

namespace ViewComponentSample.Models
{
    public class ToDoContext : DbContext
    {
        public DbSet<TodoItem> ToDo { get; set; }

        public ToDoContext(DbContextOptions options)
            :base(options)
        {
        }

    }
}

and the configuration will be injected through DI.

This is exactly what the exception says: ...If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

And you've used the AddDbContext() in your Startup.cs ConfigureServices() so the condition If AddDbContext is used in the exception has to be satisfied.

Read more about DbContext configuration here.

Hope this helps,

Regards

Upvotes: 1

Related Questions