从vs2013创建天蓝色数据库

时间:2014-03-29 15:30:41

标签: c# asp.net asp.net-mvc asp.net-mvc-4 azure

我按照本教程步骤1到5进行操作,并在本地进行检查,并且工作正常 http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started 现在我将解决方案转换为azure并发布它,我能够看到 Asp页面但是当我导航到电影(在URL中)时我得到了错误,我猜这与数据库有关,在app数据文件夹中我有本地数据库有一种方法可以转换它 azure数据库或存储,我将能够从azure网址而不是本地更新数据?

1 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是创建Azure SQL Database。请注意,在本教程中,您可能可以在步骤4停止,因为EF Code First将负责生成架构。

然后,您需要为Web.Debug.config和Web.Release.config文件设置Web.config transforms,Visual Studio将在部署时自动更改连接字符串。

在Web.config中,找到您的连接字符串:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=DatabaseName;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

(请注意,如果使用SQL Express或其他内容,您的实际连接字符串可能会有所不同)

然后将以下内容添加到Web.Debug.config和Web.Release.config:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Server={the connection string from the Azure portal}" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

确保连接字符串名称与Web.config中的名称相同,因此它知道要替换哪一个。

现在,当您在本地运行项目时,它将连接到您的本地数据库。部署到Azure时,它将连接到Azure SQL数据库。