{"id":594,"date":"2014-04-08T17:40:40","date_gmt":"2014-04-08T17:40:40","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2014\/04\/08\/ef-code-first-migrations-deployment-to-an-azure-cloud-service\/"},"modified":"2022-08-09T01:59:41","modified_gmt":"2022-08-09T08:59:41","slug":"ef-code-first-migrations-deployment-to-an-azure-cloud-service","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/ef-code-first-migrations-deployment-to-an-azure-cloud-service\/","title":{"rendered":"EF Code First Migrations Deployment to an Azure Cloud Service"},"content":{"rendered":"<p>To deploy a Code First database to an Azure Web Site, you can use the <strong>Execute Code First Migrations<\/strong> check box in the <strong>Publish Web<\/strong> wizard:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/04\/2671.blog1_thumb_2D5DB7C3.png\"><img decoding=\"async\" title=\"blog1\" border=\"0\" alt=\"blog1\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/04\/2671.blog1_thumb_2D5DB7C3.png\" width=\"695\" height=\"568\" \/><\/a> <\/p>\n<p>When you select that check box, Visual Studio configures the destination web site so that Entity Framework automatically deploys the database or updates it by running the <em>MigrateDatabaseToLatestVersion<\/em> initializer on application start, as explained in <a href=\"http:\/\/www.asp.net\/mvc\/tutorials\/deployment\/visual-studio-web-deployment\/deploying-to-iis\">this VS web deployment tutorial<\/a>.<\/p>\n<p>But if you\u2019re deploying to an Azure cloud service you don\u2019t get to use the <strong>Publish Web<\/strong> wizard. What then?<\/p>\n<p>In that case, you have two options:&#160;&#160; <\/p>\n<ul>\n<li>Write code that executes migrations when the application starts.<\/li>\n<li>Write Web.config transforms to configure the <em>MigrateDatabaseToLatestVersion<\/em> initializer to run.<\/li>\n<\/ul>\n<h2>Write App_Start code to run Migrations<\/h2>\n<p>You can run migrations manually from the <em>Application_Start<\/em> method of Global.asax with the following code:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3e63524b-6cd1-4540-91fe-fc8836de0eb7\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">var<\/span><span style=\"background:#ffffff;color:#000000\"> configuration = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> MvcWebRole.Migrations.<\/span><span style=\"background:#ffffff;color:#2b91af\">Configuration<\/span><span style=\"background:#ffffff;color:#000000\">();<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#0000ff\">var<\/span><span style=\"background:#ffffff;color:#000000\"> migrator = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">DbMigrator<\/span><span style=\"background:#ffffff;color:#000000\">(configuration);<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">migrator.Update();<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>In this snippet, <em>Configuration<\/em> is the migrations Configuration class from your Migrations folder. You can optionally specify a connection string to be used for the migration; for an example, see <a href=\"http:\/\/romiller.com\/2012\/02\/09\/running-scripting-migrations-from-code\/\">Running &amp; Scripting Migrations from Code<\/a> on Rowan Miller\u2019s blog.<\/p>\n<p>However, you probably don\u2019t want to call migrations every time the app runs, so a better solution is to use a Web.config file setting or a role environment setting to control it. For example, add the following <em>appSettings<\/em> key to the Web.config file:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7dc1b0a3-0dbe-49ef-95e1-888473706ebe\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">appSettings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">add<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">key<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">MigrateDatabaseToLatestVersion<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">value<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">true<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">\/&gt;<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">appSettings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>And only run migrations if the setting is true:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2fbeee50-0772-430e-8146-fbf9177c2b1b\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">if<\/span><span style=\"background:#ffffff;color:#000000\"> (<\/span><span style=\"background:#ffffff;color:#0000ff\">bool<\/span><span style=\"background:#ffffff;color:#000000\">.Parse(<\/span><span style=\"background:#ffffff;color:#2b91af\">ConfigurationManager<\/span><span style=\"background:#ffffff;color:#000000\">.AppSettings[<\/span><span style=\"background:#ffffff;color:#a31515\">&quot;MigrateDatabaseToLatestVersion&quot;<\/span><span style=\"background:#ffffff;color:#000000\">]))<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">var<\/span><span style=\"background:#ffffff;color:#000000\"> configuration = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> MvcWebRole.Migrations.<\/span><span style=\"background:#ffffff;color:#2b91af\">Configuration<\/span><span style=\"background:#ffffff;color:#000000\">();<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">var<\/span><span style=\"background:#ffffff;color:#000000\"> migrator = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">DbMigrator<\/span><span style=\"background:#ffffff;color:#000000\">(configuration);<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">migrator.Update();<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>With this method you can enable or disable the update-to-latest functionality locally by editing the Web.config file, and in a deployed site by using a Web.config transform. For example, you could set the setting to false for development and use the following code in Web.Release.config to enable it in production:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:73ab5776-c5f6-4180-a5b9-df16e58212bb\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">configuration<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xmlns:xdt<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">http:\/\/schemas.microsoft.com\/XML-Document-Transform<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">appSettings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">add<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">key<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">MigrateDatabaseToLatestVersion<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">value<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">true<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xdt:Locator<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Match(key)<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xdt:Transform<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">SetAttributes<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">\/&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">appSettings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">configuration<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>The <em>xdt:Locator<\/em> attribute specifies that the add element with the same key value is the one to be updated in the deployed Web.config file, and the <em>xdt:Transform<\/em> element specifies that <em>value=\u201dtrue\u201d<\/em> from the transform file will be used in the destination web site, even if <em>value=\u201dfalse\u201d<\/em> is in the Web.config file.<\/p>\n<p>(For an Azure cloud service an alternative is to use a setting in the .cscfg file and use <em>RoleEnvironment.GetConfigurationSettingValue<\/em> to retrieve the setting value.)<\/p>\n<h2>Write Web.config transforms to configure MigrateDatabaseToLatestVersion<\/h2>\n<p>Another option is to set up Web.config transforms to make the same changes to the Web.config file that Visual Studio does when you click <strong>Execute Code First Migrations<\/strong>. For example, the following sample Web.Release.config file will configure the <em>MigrateDatabaseToLatestVersion<\/em> initializer to run, and the initializer will use a connection string other than the application connection string to update the database schema. <\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c92bd635-ea5a-4c70-a6d5-a86f5a91782b\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">configuration<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xmlns:xdt<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">http:\/\/schemas.microsoft.com\/XML-Document-Transform<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">connectionStrings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">add<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">name<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">DefaultConnection<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\"><\/span><span style=\"background:#ffffff;color:#ff0000\">connectionString<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Server=tcp:server.database.windows.net,1433;Database=database;User ID=dbuser@server;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\"><\/span><span style=\"background:#ffffff;color:#ff0000\">providerName<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">System.Data.SqlClient<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xdt:Transform<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">SetAttributes<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xdt:Locator<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Match(name)<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">\/&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">add<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">name<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">DefaultConnection_DatabasePublish<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">connectionString<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Server=tcp:server.database.windows.net,1433;Database=database;User ID=dbuser@server;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">providerName<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">System.Data.SqlClient<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xdt:Transform<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Insert<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">\/&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">connectionStrings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">entityFramework<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">contexts<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">xdt:Transform<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Insert<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">context<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">type<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">MvcWebRole.Models.ApplicationDbContext, MvcWebRole<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">databaseInitializer<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">type<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MvcWebRole.Models.ApplicationDbContext, MvcWebRole], [MvcWebRole.Migrations.Configuration, MvcWebRole]], EntityFramework, PublicKeyToken=b77a5c561934e089<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">parameters<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">parameter<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">value<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">DefaultConnection_DatabasePublish<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> \/&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">parameters<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">databaseInitializer<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">context<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">contexts<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">entityFramework<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">configuration<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>This sample transform code updates the existing <em>DefaultConnection<\/em> connection string and adds a new one named <em>DefaultConnection_DatabasePublish<\/em> for the initializer to use. The code also adds to the <em>&lt;entityFramework&gt;<\/em> element a <em>&lt;contexts&gt;<\/em> element that specifies the <em>DbContext<\/em> class and specifies that the <em>MigrateDatabaseToLatestVersion<\/em> initializer should be used with that class. Note that the new connection string and the <em>&lt;contexts&gt;<\/em> element have <em>xdt:Transform=Insert<\/em> attributes.<\/p>\n<p>The code as shown works with a web project as created with the standard Visual Studio 2013 template for new web projects. If your project is different you can find the code you need as follows:<\/p>\n<ol>\n<li>Create a publish profile for publishing the web project to an Azure web site (not a cloud service).<\/li>\n<li>Select the <strong>Execute Code First Migrations<\/strong> check box and publish.<\/li>\n<li>In <strong>Server Explorer<\/strong>, expand the node for your web site under the <strong>Azure \u2013 Web Sites<\/strong> node, expand <strong>Files<\/strong>, and double-click the application Web.config file.<\/li>\n<li>You now see what Visual Studio did to enable migrations deployment, and you can copy the connection string element and the <em>&lt;contexts&gt;<\/em> element, paste them into a transform file, and add the <em>xdt:Transform<\/em> attributes.<\/li>\n<\/ol>\n<p>If your application connection string has credentials for a user that has permissions to modify the database schema, you can simplify this code by not adding a new connection string, and by specifying the application connection string in the <em>&lt;parameter&gt;<\/em> element under <em>entityFramework\/context\/context\/databaseInitializer<\/em>.<\/p>\n<h2>Choosing the right method<\/h2>\n<p>For most scenarios adding code to <em>Application_Start<\/em> is the best choice:<\/p>\n<ul>\n<li>When you\u2019re debugging, you can see and step through the code \u2013 there\u2019s no magic happening behind the scenes due to Web.config settings.<\/li>\n<li>The transform code for <em>appSettings<\/em> is much simpler than the transform code to configure an initializer. In addition, in Azure it can be even easier to change the setting because you can do it by changing the value of an environment variable.<\/li>\n<li>If you ever change your context name, your code that runs migrations doesn\u2019t change. Web.config transform code that configures an initializer would have to change.<\/li>\n<\/ul>\n<p>An advantage of the Web.config transforms shown above is that they enable you to specify a connection string for the initializer to use for schema-modification work, different from the connection string used by the application itself. You could then implement the security best practice of using credentials that have only the minimum permissions required: CRUD permissions for the application, and database owner for migrations. The <a href=\"http:\/\/romiller.com\/2012\/02\/09\/running-scripting-migrations-from-code\/\">blog mentioned earlier<\/a> shows how to do this for the code method, but of course if you do that you have more work to do in the Web.config and transform files as well as in code, so you lose some of the relative simplicity of the code method. See the discussion about multiple connection strings in <a href=\"http:\/\/www.asp.net\/mvc\/tutorials\/deployment\/visual-studio-web-deployment\/deploying-to-iis#efcfmigrations\">ASP.NET Web Deployment using Visual Studio: Deploying to Test<\/a>.<\/p>\n<h2>Summary<\/h2>\n<p>When you select the <strong>Execute Code First Migrations<\/strong> check box in the <strong>Publish Web<\/strong> wizard, Visual Studio automatically configures the destination Web.config file for migrations deployment. This post has shown some alternatives for accomplishing migrations deployment when Visual Studio doesn\u2019t display the wizard or the check box.<\/p>\n<p>Thanks to <a href=\"http:\/\/romiller.com\/\">Rowan Miller<\/a>, <a href=\"http:\/\/sedodream.com\/\">Sayed Hashimi<\/a>, and <a href=\"http:\/\/www.bradygaster.com\/\">Brady Gaster<\/a> for providing some of the code and content for this post and reviewing it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To deploy a Code First database to an Azure Web Site, you can use the Execute Code First Migrations check box in the Publish Web wizard: When you select that check box, Visual Studio configures the destination web site so that Entity Framework automatically deploys the database or updates it by running the MigrateDatabaseToLatestVersion initializer [&hellip;]<\/p>\n","protected":false},"author":424,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[],"class_list":["post-594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet"],"acf":[],"blog_post_summary":"<p>To deploy a Code First database to an Azure Web Site, you can use the Execute Code First Migrations check box in the Publish Web wizard: When you select that check box, Visual Studio configures the destination web site so that Entity Framework automatically deploys the database or updates it by running the MigrateDatabaseToLatestVersion initializer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/424"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=594"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}