在AWS上部署时,MySQL表不存在

时间:2016-10-26 00:59:02

标签: c# mysql entity-framework amazon-web-services asp.net-core

我将.net核心应用程序部署到aws,但无法弄清楚如何进行初始迁移以创建表。 在使用Code First方法时,是否有一些等同于dotnet ef migrations add Initial dotnet ef database update的aws来创建表? 我的代码在线失败:

if (context.Record.Any())
            {
                return;   // DB has been seeded
            }

2 个答案:

答案 0 :(得分:1)

通过将MySQL连接器从MySql.Data.EntityFrameworkCore更改为SapientGuardian.EntityFrameworkCore.MySql来解决问题。然后使用默认命令执行所有迁移。

答案 1 :(得分:0)

与开发相同。

您必须在应用程序启动期间调用dbContext.Database.EnsureCreated()Configure方法)。

您需要创建服务范围,然后解析DbContext

public void Configure(IApplicationBuilder app)
{
    using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
    {
        using (var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>())
        {
            if (dbContext.Database.EnsureCreated())
            {
                // Seed your database if necessary
            }
        }
    }
}

现在这是创建表的唯一方法,因为Oracle尚未实现迁移或脚手架。