添加迁移在Visual Studio 2017中不起作用

时间:2019-03-17 06:56:37

标签: asp.net-web-api visual-studio-2017 ef-migrations ef-code-first-mapping

我正在Visual Studio 2017中使用EF和代码优先方法创建Web API应用程序。我尝试使用enable-migration,最初我没有工作,所以我安装了这些软件包。

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.2.3
Install-Package Microsoft.EntityFrameworkCore.SqlServer

安装完这些软件包后,enable-migration对我有用,但是当我尝试使用Add-Migration FirstMigration命令添加迁移时,它会引发错误

  

在程序集“ UserRegistration”中找不到DbContext。确保使用正确的程序集,并且类型既不是抽象的也不是泛型的。

请帮助我如何在Visual Studio 2017中解决此问题。

enter image description here

PS:我还在DBbConnection文件中定义了web.config

<connectionString>
    <add name="DefaultConnection" 
         connectionString="Data Source=(local);Initial Catalog=WebAPIDB;Integrated Security=True" 
         providerName="System.Data.SqlClient"></add>
</connectionString>

enter image description here

1 个答案:

答案 0 :(得分:0)

    public DbSet<ModelClassName> NameForModel {get; set;} 

请在模型类中包含上面的代码,该代码具有要在数据库中迁移的属性 请参阅此链接How to apply Entity Framework code first approach to a project,以使您更容易理解。