加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – EF Core 1.1到WebApi核心 – 添加迁移失败

发布时间:2020-12-15 08:26:26 所属栏目:百科 来源:网络整理
导读:我认为我的所有依赖项都正常运行1.1但是当我尝试按照此处的步骤操作 https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db时,运行Add-Migration命令时出错. PM添加迁移InitialState 在启动类’Startup’上调用方法’ConfigureServices’
我认为我的所有依赖项都正常运行1.1但是当我尝试按照此处的步骤操作 https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db时,运行Add-Migration命令时出错.

PM>添加迁移InitialState

在启动类’Startup’上调用方法’ConfigureServices’时发生错误.

Consider using IDbContextFactory to override the initialization of the
DbContext at design-time. Error: This method could not find a user
secret ID because the application’s entry assembly is not set. Try
using the “.AddUserSecrets(Assembly assembly)” method instead. No
parameterless constructor was found on ‘ApplicationDbContext’. Either
add a parameterless constructor to ‘ApplicationDbContext’ or add an
implementation of ‘IDbContextFactory’ in the same assembly as
‘ApplicationDbContext’.

我的project.json的相关部分:

“Microsoft.EntityFrameworkCore”: “1.1.0”,“Microsoft.EntityFrameworkCore.SqlServer”: “1.1.0”,“Microsoft.EntityFrameworkCore.Design”: {
“type “: “build”,“version”: “1.1.0”
},“Microsoft.EntityFrameworkCore.Tools”: “1.1.0-preview4-final”
},“tools”: {
“Microsoft.EntityFrameworkCore.Tools.DotNet”: “1.1.0-preview4-final”
},

我的ApplicationDbContext确实有构造函数:

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

我的Startup.cs确实有:

services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”)));

还有什么呢?

解决方法

另一种解决方案:

我遇到了同样的问题并降落在这里.但我正在从头开始编写一个应用程序,将每个步骤与DotNetCore WebApp与Individual auth进行比较.在VS 2017中通过向导生成,希望遵循最新实践.因此,当我的代码和生成的代码中的所有内容完全相同时,我感到很奇怪.生成的代码在startup.cs中没有Michael Dennis的建议代码,这并不意味着他错了,它只是意味着现在有了不同的方式.在进一步深入了解后,我发现UserSecretsId在myprojetname.csproj中声明如下:

<PropertyGroup>
    <UserSecretsId>aspnet-mywebproect-006a401f-2fdc-4aad-abb1-12c00000004a</UserSecretsId>
</PropertyGroup>

在我的project.csproj文件中添加条目后,问题得以解决.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读