【问题标题】:How to use code first migrations with Azure App Service如何通过 Azure 应用服务使用代码优先迁移
【发布时间】:2018-05-23 05:28:12
【问题描述】:

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

Add-Migration Initial
Update-Database

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

我在 appsettings.json 中为 azure 添加了 connectionString,但我不明白当我发布到 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"
  }

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

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

【问题讨论】:

  • 您是否确保您的网站是 asp.net 而不是 asp.net 核心?
  • 它的 ASP.net CORE 肯定是我想要使用的,这有关系吗?
  • 没关系,我只是为了确认,因为web应用没有appsetting.json。

标签: entity-framework azure visual-studio-2017 azure-web-app-service


【解决方案1】:

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

EF 不支持自动迁移,您可能需要手动执行 Add-Migration 或 dotnet ef migrations add 来添加迁移文件。您可以显式执行命令来应用迁移,也可以apply migrations in your code

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

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

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

你会根据Environment动态选择一个连接字符串,这里是主要步骤,你可以参考一下。

  1. 在 webapp>property>debug 中将 ASPNETCORE_ENVIRONMENT 值设置为 azure。

2.关注ASP.NET Core MVC with Entity Framework Core开始。

3.用你的两个连接字符串设置appsetting.json。

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

注意:您也可以将portal数据库中的connectionstring设置到这里,然后您可以在本地进行测试,也可以使用debug进行故障排除。

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

4.在您的启动类中使用app.UseDeveloperExceptionPage();app.UseExceptionHandler 方法启用开发者异常页面,这将显示错误。

        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();
        }

更多详情可以参考这个thread

【讨论】:

  • 在哪里可以找到这个选项?在 webapp>property>debug 中将 ASPNETCORE_ENVIRONMENT 值设置为 azure
  • 你可以像我说的那样在本地设置它,右键单击你的应用程序并单击属性>调试。当您发布到 azure 时,该设置也适用。
  • 是的,但我不仅有调试配置文件,我还需要多个配置文件,而不仅仅是开发和生产,我不知道如何在您的解决方案中添加更多环境,例如测试、开发、质量检查、产品
  • 我知道您有多个个人资料。 ASPNETCORE_ENVIRONMENT 值代表您要使用的当前配置文件。所有具有不同设置的配置文件都在 ConfigureServices 中。当你的 ASPNETCORE_ENVIRONMENT 值为 test 时,它会做出判断并进入 test 连接字符串。
  • 我明白,但我不明白我在哪里添加环境
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
  • 2013-11-05
  • 2016-05-26
  • 1970-01-01
相关资源
最近更新 更多