How to use code first migrations with Azure App Service

假装没事ソ 提交于 2020-01-14 04:41:09

问题


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

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?

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. Local
  2. Published to AzureApp-Test
  3. Published to AzureApp-Prod

回答1:


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

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.

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. Set the ASPNETCORE_ENVIRONMENT value to azure in webapp>property>debug.

2.Follow ASP.NET Core MVC with Entity Framework Core to get started.

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.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.



来源:https://stackoverflow.com/questions/50480435/how-to-use-code-first-migrations-with-azure-app-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!