1

我无法运行迁移以创建/更新托管在 Azure 上的 Sql Db。它们在我的 LocalDb 本地运行良好,但在发布到 Azure 时似乎完全被忽略了。

详细信息:- Asp.Net MVC 5.2.3 - Entity Framework 6.1.3 - Visual Studio Team Services(在线)- Azure Web App 和 Azure SQL Db(在 Imagine / Dreamspark 订阅下)

我创建了一个不使用 CI 的不同应用程序,但使用了 Visual Studio 中的发布功能,它发布得很好并且工作正常。我已经从 Azure 中删除了该 Web 应用程序和数据库,因为订阅只允许一个数据库。

我想这可能是我在 VSTS 中设置构建定义时缺少的东西,我只是不确定是什么。

我努力了:

...以及其他一些人(我不再打开窗户,否则我也会链接它们)。

我当前的构建步骤包括:

  1. NuGet 还原 - NuGet 安装程序
  2. 构建解决方案 - Visual Studio 构建
  3. 测试程序集 - Visual Studio 测试
  4. 发布符号路径 - 索引源和发布符号
  5. 发布工件 - 发布构建工件

在我的本地环境中,EF 在我启用迁移、添加迁移、更新数据库时起作用。我猜上述步骤都没有做到这一点,所以我需要一个或多个步骤。Ben Day 的博客似乎应该可以工作,但由于某种原因,它没有找到我的 bin/release 文件。

在发布上述成功的构建定义后,实际的应用程序就可以很好地部署到 Azure。只是数据库被完全忽略了。使用 SSMS,我检查了 Azure 数据库,虽然它存在,但它没有我的表或数据。

我错过了什么?

谢谢。

**编辑 - 找到了我丢失的简单项目。我需要将数据库的连接字符串添加到 Azure 中的 Web 应用程序。它现在有我的初始表,并使用这些表存储数据,但不进行迁移。所以我在过去两天所做的一些事情让我的初始表格在那里。现在..记住它是什么。

**第二次编辑 - 所以,我认为在我修复 VSTS 中的连接字符串之前,这些表已进入发布数据库。我尝试的其中一件事是创建一个单独的数据库项目。我想当我在某个时候推送时,当时存在的表也推送到释放,我只是看不到它们,因为我没有将应用程序和数据库连接在一起?无论如何,EF 迁移仍未被识别。我再次尝试了 Ben Day 的建议,但出现了构建错误:

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.

我正在使用 EF 6.1.3

4

1 回答 1

1

我也遇到了这个。我正在使用 Visual Studio 发布我的 Azure 应用服务。在做了一些研究之后(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),当您在“发布”设置中选中“更新数据库”复选框时,我了解到 VS Publish 会转换您的 Web 配置,添加一个连接字符串,它引用包含您的 _migrations 表的数据库,以及“entityFramework”下的“contexts”元素。您可以通过查看“..obj\Release\InsertEFCodeFirstDeploy\transformed\web.config”来查看之前发布对 web.config 所做的事情。

因此,为了在 VSTS 中使用构建和发布定义时重新创建相同的操作,我在我的服务项目的 Web.Release.config 中添加了 2 个 XDT“插入”转换。我的看起来像这样:

<?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>

接下来,您需要转到 Azure 门户中的应用服务“应用程序设置”并添加一个名称为“MS_TableConnectionString_DatabasePublish”的连接字符串,并将其值设置为与“MS_TableConnectionString”中应该已经存在的值相同的值设置。

于 2018-07-01T17:20:54.363 回答