简体   繁体   English

ASP.NET Core + Entity Framework Core + SQLite 创建初始迁移引发错误

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

I am doing a ASP.NET Core beginner tutorial on Udemy.我正在 Udemy 上做一个 ASP.NET Core 初学者教程。 The task is to create a Code First SQLite database and I want to create a Initial migration which is failing.任务是创建一个 Code First SQLite 数据库,我想创建一个失败的初始迁移。 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 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我正在将DataContext注册为服务

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配置文件中的配置是application.json文件

{
  "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我收到错误迁移错误消息 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?我的问题:出了什么问题?

I found that in the DataContext I used the wrong parameter我发现在DataContext我使用了错误的参数

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

should be应该

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

Migration executed as expected.迁移按预期执行。

在此处输入图片说明

Change your DataContext Class like this:像这样更改您的 DataContext 类:

 using Microsoft.EntityFrameworkCore;
 public class DataContext : DbContext {           
   public DataContext(DbContextOptions<DataContext> options) : base (options) { }    
   public DbSet<Values> Values { get; set; }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM