使用 Azure 上的现有数据库部署 ASP 个 MVC 应用程序

Deploy ASP MVC application with existing database on Azure

我在 App_Data 文件夹中定义了数据库。是否可以使用包含 App_Data if 文件夹的数据库部署我的应用程序?或者自动更新 azure db?或者如果不可能手动更新 azure db 的最佳方法是什么?

进入你的项目目录,打开*.csproj文件,在Project标签下添加以下代码。

<ItemGroup>
   <Content Include="App_Data\*">
      <CopyToPublishDirectory>always</CopyToPublishDirectory>
   </Content>
</ItemGroup>

然后重新发布,App_Data文件夹下的所有文件都会上传到azure

我们还可以在 VS 中更新我们的 Azure Db:Tutorial: Build an ASP.NET app in Azure with SQL Database

第一个解决方案

如果你使用 entity framework,你可以直接更新,第一步你必须替换 Web.config 中的数据库连接字符串:

 <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=OnlineAuctionDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OnlineAuctionDb.mdf" providerName="System.Data.SqlClient"/>

通过 Azure 中的数据库连接字符串

<add name="DefaultConnection" connectionString="Data Source=xxxxxxx.database.windows.net,1433;Initial Catalog=database;User Id=user@xxxxxxxx.database.windows.net;Password=mypassword" providerName="System.Data.SqlClient"/>

如果你发布后检查这个值 "selected Run the code First Migrations" 正如你在这张图片中看到的

在最后一步中,您单击“发布”按钮

第二种解决方案

您应该从本地数据库中获取 SQL 脚本,并在远程数据库中 运行 获取它。 (不要忘记更改 web.config 中的连接字符串以在发布前进行测试)