【问题标题】:How can I Update-Database?如何更新数据库?
【发布时间】:2021-10-13 23:34:47
【问题描述】:

这是用于生成迁移的代码

dotnet ef migrations add InitialCreate --context DataContext --output-dir Migrations/SqlServerMigrations 这是DataContext.cs

namespace WebApi.Helpers
{
    public class DataContext : DbContext
    {
        protected readonly IConfiguration Configuration;

        public DataContext(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            // connect to sql server database
            options.UseSqlServer(Configuration.GetConnectionString("WebApiDatabase"));
        }

        public DbSet<User> Users { get; set; }
    }
}

但是当我跑步时 Upadate-Database 我得到错误 “找到了多个 DbContext。指定要使用哪一个。对 PowerShell 命令使用“-Context”参数,对 dotnet 命令使用“--context”参数。”

如何使用此迁移更新数据库?

【问题讨论】:

    标签: c# .net entity-framework migration package-manager-console


    【解决方案1】:
    Update-Database --context DataContext
    

    作为ef migrations add 的一部分,您将上下文指定为DataContext。我相信上面的命令应该允许你相应地更新数据库。

    【讨论】:

    • 我现在收到此错误“更新数据库:找不到接受参数 'DataContext' 的位置参数。”
    • 修复它只需要使用-context 而不是--context,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2017-03-04
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多