微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我认为我的所有依赖项都正常运行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文件添加条目后,问题得以解决.

原文地址:https://www.jb51.cc/csharp/243576.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐