简体   繁体   English

EF Core 1.1到WebApi核心 - 添加迁移失败

[英]EF Core 1.1 to WebApi Core - Add-Migration fails

I think I've got all my dependencies running 1.1 properly but when I try to follow the steps here https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db I get an error running the Add-Migration command. 我想我的所有依赖项都正常运行1.1但是当我尝试按照这里的步骤进行操作时https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db我得到了运行Add-Migration命令时出错。

PM> Add-Migration InitialState PM> Add-Migration InitialState

An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. 在启动类'Startup'上调用方法'ConfigureServices'时发生错误。

Consider using IDbContextFactory to override the initialization of the DbContext at design-time. 考虑使用IDbContextFactory在设计时覆盖DbContext的初始化。 Error: This method could not find a user secret ID because the application's entry assembly is not set. 错误:此方法找不到用户密码ID,因为未设置应用程序的条目程序集。 Try using the “.AddUserSecrets(Assembly assembly)” method instead. 请尝试使用“.AddUserSecrets(Assembly assembly)”方法。 No parameterless constructor was found on 'ApplicationDbContext'. 在'ApplicationDbContext'上找不到无参数构造函数。 Either add a parameterless constructor to 'ApplicationDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationDbContext'. 要么将无参数构造函数添加到'ApplicationDbContext',要么在与'ApplicationDbContext'相同的程序集中添加'IDbContextFactory'的实现。

relevant sections of my project.json: … 我的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”
},

My ApplicationDbContext does have the constructor: 我的ApplicationDbContext确实有构造函数:

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

and my Startup.cs does have the: 我的Startup.cs确实有:

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

What else can it be? 还有什么呢?

The issue is related to the builder.AddUserSecrets() call. 该问题与builder.AddUserSecrets()调用有关。 To fix perform the following steps: 要修复,请执行以下步骤:

  1. Adding the user secret to the assembly (instead of just project.json ) by adding attribute [assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")] to Startup.cs 通过添加属性添加用户秘密到组件(而不是仅仅project.json) [assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")]Startup.cs

  2. In Startup.cs replace builder.AddUserSecrets() with builder.AddUserSecrets<Startup>(); Startup.cs中,builder.AddUserSecrets<Startup>();替换builder.AddUserSecrets() builder.AddUserSecrets<Startup>();

Reference: InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly 参考: InvalidOperationException:无法在程序集中找到“UserSecretsIdAttribute”

Your issue is not related to EF Core, it's about User Secrets 您的问题与EF Core无关,它与用户秘密无关

Check your Startup.cs constructor - it contains AddUserSecrets() call. 检查你的Startup.cs构造函数 - 它包含AddUserSecrets()调用。 You can remove it. 你可以删除它。 Later, when you read about User Secrets, you can add it back with correct configuration (I guess you have website template from 1.0.0, while referencing library 1.1.0 - it contains this bug fix ) 稍后,当您阅读有关用户秘密的内容时,您可以使用正确的配置将其添加回来(我猜您有1.0.0的网站模板,同时引用库1.1.0 - 它包含此错误修复

An alternate solution: 另一种解决方案:

I ran into the same problem and landed here. 我遇到了同样的问题并降落在这里。 But I am writing an app from scratch comparing it at every step with the DotNetCore WebApp with Individual auth. 但我正在从头开始编写一个应用程序,将每个步骤与DotNetCore WebApp与Individual auth进行比较。 that gets generated via wizard in VS 2017 hoping to follow latest practices. 在VS 2017中通过向导生成,希望遵循最新实践。 So it was strange when everything was identical in my code and the generated code and I was getting this exception. 因此,当我的代码和生成的代码中的所有内容完全相同时,我感到很奇怪。 The generated code did not have Michael Dennis's suggested code in startup.cs, which does not mean he is wrong, it just means there was now a different way. 生成的代码在startup.cs中没有Michael Dennis的建议代码,这并不意味着他错了,它只是意味着现在有了不同的方式。 Upon diving down further I found out that UserSecretsId was declared in myprojetname.csproj like follows: 在进一步深入了解后,我发现UserSecretsId在myprojetname.csproj中声明如下:

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

After adding the entry in my project.csproj file, the issue was resolved. 在我的project.csproj文件中添加条目后,问题得到了解决。

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

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