【问题标题】:MVC Entity framework from local database to SQL database Azure从本地数据库到 SQL 数据库 Azure 的 MVC 实体框架
【发布时间】:2025-11-23 17:15:01
【问题描述】:

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

在我的项目 MVC Entity 框架中,在我的本地计算机上,它与本地数据库连接,并且按预期工作。我可以将项目添加到数据库并删除其等。在 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>

我做错了什么?

【问题讨论】:

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


【解决方案1】:

睡了一晚,终于搞定了。首先,我必须明确我将在哪个数据库中初始化 DbContext

public class EFDbContext : DbContext
{

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

    }
}

InventoryEntities 是我没有更改的 WebUI 的 web.config 中的连接字符串的名称。

就这么简单,但花了好几天......

【讨论】:

    最近更新 更多