1

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
huysmania
  • 1,054
  • 5
  • 11
Matt Douhan
  • 2,053
  • 1
  • 21
  • 40

1 Answers1

4

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.

enter image description here

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.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • where do I find this option? Set the ASPNETCORE_ENVIRONMENT value to azure in webapp>property>debug – Matt Douhan May 23 '18 at 05:54
  • You could set it in local as I have said, right click your app and click property>debug. When you publish to azure, the setting also apply. – Joey Cai May 23 '18 at 05:59
  • Yes but I don't only have debug profile, I need multiple profiles, not just development and production, I don't see how I can add more environments in your solution, like Test, Dev, QA, Prod – Matt Douhan May 23 '18 at 06:00
  • I know you have multiple profiles. The ASPNETCORE_ENVIRONMENT value is represent the current profile you want to use. And all your profile with different settings is in ConfigureServices. When your ASPNETCORE_ENVIRONMENT value is test, it will make judgement and go to the test connectionstring. – Joey Cai May 23 '18 at 06:08
  • I understand that, but I don't understand where I add the environments – Matt Douhan May 23 '18 at 06:09
  • What's the meaning of environments you want to add? – Joey Cai May 23 '18 at 06:10
  • you call it profile I think, for Test profile I want use connection string 1, for dev profile I want use connection string 2, but where do I add those profiles? – Matt Douhan May 23 '18 at 06:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171595/discussion-between-joey-cai-and-matt-douhan). – Joey Cai May 23 '18 at 06:13