1
votes

I am having trouble getting migrations to run to create / update my Sql Db hosted on Azure. They run fine locally to my LocalDb but seem to be completely ignored when releasing to Azure.

Details: - Asp.Net MVC 5.2.3 - Entity Framework 6.1.3 - Visual Studio Team Services (online) - Azure Web App & Azure SQL Db (under Imagine / Dreamspark subscription)

I created a different application that didn't use CI, but used the Publish feature within Visual Studio and it published fine and worked. I have since removed that web app & db from Azure since the subscription only allows one db.

I'm thinking it may be something I'm missing in setting up the build definition in VSTS, I'm just not sure what.

I have tried:

... as well as a bunch of others (I don't have the windows open any longer or I'd link them too).

My current build steps include:

  1. NuGet restore - NuGet Installer
  2. Build solution - Visual Studio Build
  3. Test Assemblies - Visual Studio Test
  4. Publish symbols path - Index Sources & Publish Symbols
  5. Publish Artifact - Publish Build Artifacts

In my local env, EF works when I Enable-Migrations, Add-Migration, Update-Database. I'm guessing none of the above steps do this so I need one or more steps. Ben Day's blog seems like it should work, but for some reason, it's not finding my bin/release file.

The actual application deploys to Azure just fine upon release of the above successful build definition. It's just that the database is completely ignored. Using SSMS, I check the Azure db, and while it exists, it has none of my tables or data.

What am I missing?

Thanks.

**Edit - found the simple item I was missing. I need to add the connection string for the db to the web app in Azure. It now has my initial tables, and stores data using those tables, but doesn't pick up migrations. So something I've done in the last two days got my initial tables out there. Now.. to remember what it was.

**2nd Edit - So, I think the tables made it into the release db before I fixed my connection string in VSTS. One of the things I tried was creating a separate database project. I think when I pushed that at some point, the tables in existence at that time also pushed to release, I just couldn't see them because I hadn't connected the application and db together? At any rate, EF migrations are still not being recognized. I tried Ben Day's suggestion again, but I"m getting the build error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

I am using EF 6.1.3

1
What is your bin folder looking like? do you have the EF assemblies in the output? (Copy local to true)baywet
My bin folder on my local machine doesn't have a release folder (which means I guess it's not there on VSTS either, and it was failing on that step), so for the step that looks for the release configuration, I pointed it to the obj folder (which has both debug and release folders) - that allowed the process outlined by Ben to get past that step. Yes, EF is set to Copy Local true.user2951579
I think you should make sure you have a publish build artifacts steps targeting the staging directory at the end of the build definition. Then can you run the build again and export a text version of the file tree you have there? -file -dir-filebaywet

1 Answers

1
votes

I ran into this as well. I was using Visual Studio to publish my Azure app service. After doing some research (https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application#deploy-to-azure), I came to learn that VS Publish transforms your web config when you check the box for "Update Database" in Publish setting, adding a connection string that refers to the database containing your _migrations table, as well as a "contexts" element under "entityFramework". You can see what a previous publish has done with your web.config by looking at "..obj\Release\InsertEFCodeFirstDeploy\transformed\web.config".

So to recreate this same action when using a build and release definition in VSTS, I added 2 XDT "insert" transforms in Web.Release.config in my service project. Mine looks like this:

<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=301874 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MS_TableConnectionString_DatabasePublish" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=AzureStorageEmulatorDb45;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True" 
         providerName="System.Data.SqlClient" xdt:Transform="Insert" />
  </connectionStrings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
  <entityFramework>
    <contexts xdt:Transform="Insert">
      <context type="helpmeshopService.Models.helpmeshopContext, helpmeshopService">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[helpmeshopService.Models.helpmeshopContext, helpmeshopService], [helpmeshopService.Migrations.Configuration, helpmeshopService]], EntityFramework, PublicKeyToken=b77a5c561934e089">
          <parameters>
            <parameter value="MS_TableConnectionString_DatabasePublish" />
          </parameters>
        </databaseInitializer>
      </context>
    </contexts>
  </entityFramework>
</configuration>

Next, you will need to go to your App Service "Application Settings" in the Azure portal and add a connection string with name, "MS_TableConnectionString_DatabasePublish", and set its value to the same value as for "MS_TableConnectionString" that should already exist in your settings.