entity framework - Using ASP.NET Core and EntityFramework 7 to update production DB with migration changes in Azure -


i have complete web app working locally, , working on getting going in production. app sitting on azure, , watching git repo new deployments, working great.

however, app has connection in appsettings.json connection string, looks 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 fine local testing, i'm ready move production , have questions (couldn't find documentation).

  1. how use dnx command line push changes production db? tied db defined app, statically, it'll default go local db, always.

  2. once db provisioned in azure, need modify connection string in settings on azure web app, this?

enter image description here

basics

you've said have 2 configs. 1 local, 1 production, connection string in each. in case, if production config called appsettings.production.json can 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 update production database.

advanced

integrate continuous integration. have migration update live database on build adding postbuild script project.json. azure run on each deployment.

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

if want use environmental variables instead of appsettings.production.json file, checkout setting sql connection string asp.net 5 web app in azure. keeps username/password out of git repo.

references


Comments