如何在发布期间使用不同的连接字符串更新数据库

How to update database during publish using different connection string

我有一个数据库需要在将 Web 应用程序发布到远程服务器时进行更新。我想要一个本地版本的数据库和一个远程(已发布)版本,这样我就可以在不影响已发布数据库的情况下对本地版本进行模式更改。

我最初在我的项目的本地副本中使用了远程数据库连接字符串,它发布得很好。我已经将此连接字符串更改为本地数据库,并在我的发布设置中指定了远程连接字符串,勾选了 'Use this connection string at runtime' 和 'Update database' 选项。

现在,当我尝试发布时,出现此错误:

Web deployment task failed. (Could not generate deployment script.
Internal Error. The database platform service with type Microsoft.Data.Tools.Schema.Sql.Sql130DatabaseSchemaProvider is not valid. You must make sure the service is loaded, or you must provide the full type name of a valid database platform service.
Internal Error. The database platform service with type Microsoft.Data.Tools.Schema.Sql.Sql130DatabaseSchemaProvider is not valid. You must make sure the service is loaded, or you must provide the full type name of a valid database platform service.
  Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_EXECUTING_METHOD.)

两个连接字符串都绝对有效 - 我已经使用它们更新了两个数据库。我仅使用远程连接字符串就成功发布了。当我在本地 Web.config 中提到不同的连接字符串(即我的本地数据库)时,它似乎只会失败。如果我将本地 Web.config 连接字符串改回远程连接字符串,它会再次工作。

抱歉,如果这是重复的 - 我已经在网上查看了解决方案,但没有发现任何与我遇到的问题相似的内容。

我认为部分问题是数据库是本地的,并且在连接字符串中使用 (LocalDb) - 我假设发布时无法连接到它。我将其更改为另一台服务器上的数据库,并手动将发布设置编辑为(有效地)这些设置,现在它可以正常工作了。

.pubxml 文件:

<ObjectGroup Name="SomeDatabaseContext" Order="1" Enabled="True">
  <Destination Path="Data Source=TestServer;Initial Catalog=SomeDatabase;Persist Security Info=True;User ID=AppUser;Password=SomePassword;Application Name=EntityFramework" Name="Data Source=TestServer;Initial Catalog=SomeDatabase;Persist Security Info=True;User ID=AppUser;Password=SomePassword;MultipleActiveResultSets=True;Application Name=EntityFramework" />
  <Object Type="DbDacFx">
    <PreSource Path="Data Source=DevServer;Initial Catalog=SomeDatabase.dev;Persist Security Info=True;User ID=AppUser;Password=SomePassword;Application Name=EntityFramework" includeData="False" />
    <Source Path="$(IntermediateOutputPath)AutoScripts\ECommerceSiteContext_IncrementalSchemaOnly.dacpac" dacpacAction="Deploy" />
  </Object>
  <UpdateFrom Type="Web.Config">
    <Source MatchValue="Data Source=DevServer;Initial Catalog=SomeDatabase.dev;Persist Security Info=True;User ID=AppUser;Password=SomePassword;Application Name=EntityFramework" MatchAttributes="$(UpdateFromConnectionStringAttributes)" />
  </UpdateFrom>
</ObjectGroup>

Web.config:

<connectionStrings>
  <add name="SomeDatabaseContext" connectionString="Data Source=DevServer;Initial Catalog=SomeDatabase.dev;Persist Security Info=True;User ID=AppUser;Password=SomePassword;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

这样做允许我在网站上工作时使用数据库的开发版本(可能需要更改数据库架构)而不影响测试(已发布)版本,并且允许我发布更改而无需发布时记得Update-Database针对测试数据库。