ASP.NET Core + Entity Framework Core + SQLite Creating initial migration throws error

asp.net-core ef-migrations entity-framework-core sqlite

Question

I am doing a ASP.NET Core beginner tutorial on Udemy. The task is to create a Code First SQLite database and I want to create a Initial migration which is failing. Below I describe the steps I have made.

I have installed the packages

Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SQLite

The Model is

public class Value
{
    public int Id { get; set; }
    public string Name { get; set; }
}

The DbContext

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DbContext> options) : base(options) {}
    public DbSet<Value> Values { get; set; }
}

I am registering the DataContext as a service

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<DataContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
        services.AddControllers();
    }

    //...
}

Where the configuration is in the configuration file is the application.json file

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=wapp.db"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Now when I do the first migration call

dotnet ef migrations add InitialCreate

I get the error Migration error message Here as text

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: NWebApp.API.Data.DataContext Lifetime: Scoped ImplementationType: NWebApp.API.Data.DataContext': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Microsoft.EntityFrameworkCore.DbContext]' while attempting to activate 'NWebApp.API.Data.DataContext'.)
Unable to create an object of type 'DataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

My question: What is going wrong?

1
0
3/30/2020 12:44:40 PM

Popular Answer

I found that in the DataContext I used the wrong parameter

public DataContext(DbContextOptions<DbContext> options) : base(options) {}

should be

public DataContext(DbContextOptions<DataContext> options) : base(options) {}

Migration executed as expected.

enter image description here

0
3/30/2020 12:44:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow