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>