gpt4 book ai didi

c# - EF Core 1.1 到 WebApi Core - 添加迁移失败

转载 作者:太空狗 更新时间:2023-10-29 21:26:35 26 4
gpt4 key购买 nike

我想我的所有依赖项都可以正常运行 1.1,但是当我尝试按照此处的步骤操作时 https://learn.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 theDbContext at design-time. Error: This method could not find a usersecret ID because the application’s entry assembly is not set. Tryusing the “.AddUserSecrets(Assembly assembly)” method instead. Noparameterless constructor was found on ‘ApplicationDbContext’. Eitheradd a parameterless constructor to ‘ApplicationDbContext’ or add animplementation 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”)));

还能是什么?

最佳答案

此问题与 builder.AddUserSecrets() 有关称呼。要修复,请执行以下步骤:

  1. 通过添加属性 [assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")] 将用户 secret 添加到程序集(而不仅仅是 project.json)到 Startup.cs

  2. Startup.cs 中替换 builder.AddUserSecrets()builder.AddUserSecrets<Startup>();

引用:InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly

关于c# - EF Core 1.1 到 WebApi Core - 添加迁移失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40858155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com