MVC实体框架从本地数据库到SQL数据库Azure

时间:2017-04-29 20:26:27

标签: sql asp.net-mvc entity-framework azure localdb

可能是一个非常简单的问题,但它已经花了我好几天。问题:

在我的项目中,MVC实体框架,在我的本地计算机上,它与本地数据库连接,并按预期工作。我可以添加项目到数据库并删除它等。在azure,我有一个SQL数据库,当我将项目部署到azure时,我收到以下错误消息

network-related or instance-specific error occurred while establishing a 
connection to SQL Server. The server was not found or was not accessible. 
Verify that the instance name is correct and that SQL Server is configured to 
allow remote connections. (provider: SQL Network Interfaces, error: 52 - 
Unable to locate a Local Database Runtime installation. Verify that SQL 
Server Express is properly installed and that the Local Database Runtime 
feature is enabled.)]

我认为我的连接字符串不正确,我的应用程序无法连接到azure SQL数据库。我试图添加和更改连接字符串,但不知怎的,我无法使它工作。这就是我所拥有的(减去密码和用户名):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings><add name="InventoryEntities" connectionString="metadata=res://*/Administration.csdl|res://*/Administration.ssdl|res://*/Administration.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=tcp:****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
</configuration>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

经过一夜的睡眠,我终于让它工作了。首先,我必须弄清楚我将使用哪个数据库来初始化DbContext

public class EFDbContext : DbContext
{

    public EFDbContext() : base("name=InventoryEntities")
    {

    }
}

InventoryEntities是WebUI的web.config中的connectionstring的名称,我没有更改。

这很简单,但需要几天......