如何通过Azure App Service使用代码优先迁移 [英] How to use code first migrations with Azure App Service

查看:111
本文介绍了如何通过Azure App Service使用代码优先迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本地运行时,我会手动运行以下命令,然后将应用打包并发布到IIS服务器上.

When I run locally I run the commands below manually and then package and publish the app onto my IIS server.

Add-Migration Initial
Update-Database

当我想发布到azure appservice时,这些命令会自动运行吗?如果是这样,当我将其发布到azure时,如何知道使用其他ConnectionString?

When I want to publish to an azure appservice will these commands run automatically? If so how does it know to use a different ConnectionString when I publish it to azure?

我在appsettings.json中添加了用于Azure的connectionString,但是当我发布到Azure AppServices时,我不明白如何告诉控制器等使用它.

I added the connectionString for azure in appsettings.json but I don't understand how I can tell my controllers etc to use that when I publish to azure AppServices

"ConnectionStrings": {
    "AzureTestConnection": "Data Source=tcp:xxxxxx-test.database.windows.net,1433;Initial Catalog=xxxxx;User Id=xxx@yyyy.database.windows.net;Password=xxxxxx",
    "NWMposBackendContext": "Server=(localdb)\\mssqllocaldb;Database=NWMposBackendContext-573f6261-6657-4916-b5dc-1ebd06f7401b;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

我正在尝试使用三个具有不同连接字符串的配置文件

I am trying to have three profiles with different connection strings

  1. 本地
  2. 发布到AzureApp-Test
  3. 发布到AzureApp-Prod

推荐答案

当我想发布到azure appservice时,这些命令会自动运行吗?

When I want to publish to an azure appservice will these commands run automatically?

EF不支持自动迁移,您可能需要手动执行Add-Migration或dotnet ef迁移添加以添加迁移文件.您可以显式执行命令以应用迁移,也可以

EF does not support Automatic migrations, you may need to manually execute Add-Migration or dotnet ef migrations add for adding migration files. You could explicitly execute the command to apply the migrations, also you could apply migrations in your code.

您可以在Startup.cs文件的Configure方法中添加以下代码:

And you could add the following code in the Configure method of Startup.cs file:

using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
    scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
}

我正在尝试使用三个具有不同连接字符串的配置文件

I am trying to have three profiles with different connection strings

您将根据环境动态选择连接字符串,因此这里是主要步骤,您可以参考它.

You would dynamically choose a connection string based on Environment, so here is main steps, you could refer to it.

  1. 在webapp>属性>调试中将ASPNETCORE_ENVIRONMENT值设置为天蓝色.

2.按照 ASP.NET Core带有Entity Framework Core的MVC 入门.

3.使用两个连接字符串设置appsetting.json.

3.Set the appsetting.json with your two connection string.

{
  "ConnectionStrings": {
    "DefaultConnection": "connectiondefault",
    "azure": "connectionazure"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

注意:您还可以将门户网站上数据库中的连接字符串设置为此处,然后可以在本地对其进行测试,并可以使用调试来进行故障排除.

Note:You could also set the connectionstring in database on portal to here, then you could test it in local and could use debug to troubleshooting.

此外,您可以尝试使用一个连接字符串进行测试,以确保连接数据库没有问题.

Also, you could try to test with one connectionstring to ensure you have no problem with connecting to database.

4.通过在启动类中使用app.UseDeveloperExceptionPage();app.UseExceptionHandler方法启用开发人员异常页面,该页面将显示错误.

4.Enable Developer exception page by using app.UseDeveloperExceptionPage(); and the app.UseExceptionHandler methods in your startup class which would display the errors.

        public Startup(IHostingEnvironment env)
        {
            Configuration = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .Build();

            HostingEnvironment = env;
        }

        public IConfigurationRoot Configuration { get; }
        public IHostingEnvironment HostingEnvironment { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            if (HostingEnvironment.IsDevelopment())
            {
                services.AddDbContext<SchoolContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            }
            else
            {
                services.AddDbContext<SchoolContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("azure")));
            }
            services.AddMvc();
        }

有关更多详细信息,您可以参考此

For more details, you could refer to this thread.

这篇关于如何通过Azure App Service使用代码优先迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆