4

I have a complete web app working locally, and I am now working on getting it going in production. The app is currently sitting on Azure, and watching a git repo for new deployments, which is working great.

However, the app has a connection in its appsettings.json for a connection string, which looks like this:

"database": {
  "connection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Foo"
},

// In Startup()
var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables();

// In ConfigureServices(...)
services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<FooDbContext>(options =>
        {
            options.UseSqlServer(Configuration["database:connection"]);
        });

This is fine for local testing, but now I'm ready to move to production and have some questions (couldn't find good documentation).

  1. How do I use the dnx command line to push my changes to a production DB? Everything is tied to the DB defined by the app, statically, so it'll by default go to my local DB, always.

  2. Once the DB is provisioned in Azure, do I simply need to modify my connection string in the settings on Azure for the web app, like this?

enter image description here

5
  • This post explains EF7 migrations with dnx. Check it msdn.microsoft.com/en-us/magazine/mt614250.aspx Commented Apr 6, 2016 at 16:42
  • 1
    That article doesn't explain how to apply a database update to a production DB. Commented May 2, 2016 at 13:43
  • Are you (also) working with migrations locally? Commented May 2, 2016 at 14:18
  • Yes, I have two configs. One for local, one for production, with a connection string in each. Commented May 2, 2016 at 14:20
  • Are you using RC1 or RC2? Commented May 3, 2016 at 1:23

1 Answer 1

6
+50

Basics

You've said that you have two configs. One for local, one for production, with a connection string in each. In that case, if your production config is called appsettings.production.json you can do this:

Startup()

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables();

ConfigureServices(...)

services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<FooDbContext>(options =>
    {
        options.UseSqlServer(Configuration["database:connection"]);
    });

appsettings.production.json

{
  "database": {
    "connection": "Server=tcp:yr3d5dswl.database.windows.net,1433;Database=EFMigrationDemo;User ID=mvp2015@yr3d5dswl;Password=3f4g%^BD45bcE;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  }
}

Command Line

dotnet ef database update -e Production

This will update your production database.

Advanced

Integrate with your continuous integration. Have the migration update the live database on build by adding a postbuild script to your project.json. Azure will run this on each deployment.

"scripts": {
  "postbuild": [ "dotnet ef database update" ]
}

If you want to use environmental variables instead of a appsettings.production.json file, checkout Setting the SQL connection string for ASP.NET 5 web app in Azure. This keeps your username/password out of your git repo.

References

Sign up to request clarification or add additional context in comments.

9 Comments

How would you test a production Azure SQL connection string locally? I set an environment variable of set database:connection=my_production_connection_string and my Configuration.Get<string>("database:connection") fails to find any environment variable. If I inspect the environment variables, I see one with this though: Data:database:connection:ConnectionString. with the same value that I just set for database:connection. Why is it transformed like that? And how can I possibly access that from my code while still allowing my Development environment to work too?
What method are you using to inspect environmental variables?
@mariocatch While I am not sure why it is transformed, it does seem related to conventions. Try accessing it with Configuration.Get("Data:database:connection"). Also set your config.json value to "Data" : { "database" : { "connection" : "..." } }. There are more details here stackoverflow.com/questions/31097933/…
Yep, the key looks to be to use the format "Data": { "my_db": { "ConnectionString": "..." } }. I am testing how this works from Azure now and will respond back if there's an issue.
Looks like the postbuild step of dnx ef database update isn't running. My DB is not created with that step in there. Also, after inspecting the Kudu SCM environment variables, the connection string that I setup in my Azure Web App connection strings UI is: SQLAZURECONNSTR_Data:my_database:ConnectionString. So, that may be why all my connection string lookups are failing in production? Although I think the provider is supposed to trim the azure prefix off and still find/use it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.