【问题标题】:Connection problem with SQL Server using Entity Framework in Visual Studio在 Visual Studio 中使用实体框架与 SQL Server 的连接问题
【发布时间】:2018-11-02 13:21:23
【问题描述】:

错误:

与 SQL Server 建立连接时出现与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定实例)

这是引发错误的函数

public void AddDatos(LectorDatos ld, int sensorId)
{
        using (Model.CEREBROEntities context = new Model.CEREBROEntities())
        {
            Motor m = (Motor)ld;

            Model.Motor motor = new Model.Motor()
            {
                SensorID = sensorId,
                Date = DateTime.Now,
                PresionAceite = m.PresionAceite,
                TempMotor = m.TempMotor
            };

            context.Motor.Add(motor);
            context.SaveChanges();

            return;
        }
}

连接字符串是使用 ADO.NET 实体数据模型自动生成的,该模型使用有效的数据库连接(按下“测试连接”按钮会给出积极的反馈)。我将模型命名为 CEREBRO,并将连接字符串命名为 CEREBROEntities。

<configuration>
    <configSections>
        <section name="entityFramework"  
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="CEREBROEntities" 
             connectionString="metadata=res://*/Model.CEREBRO.csdl|res://*/Model.CEREBRO.ssdl|res://*/Model.CEREBRO.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=CASCA-PC\SQLEXPRESS;initial catalog=CEREBRO;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
             providerName="System.Data.EntityClient" />
    </connectionStrings>
    <entityFramework>
        <defaultConnectionFactory  type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
            <providers>
                <provider invariantName="System.Data.SqlClient" 
                          type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            </providers>
    </entityFramework>
</configuration>

我也可以使用带有 windows 身份验证和 SQL Server 身份验证的 Microsoft SQL Server Management Studio 连接到服务器,但它们都不能在 Visual Studio 上运行。

我尝试过的事情

  • 服务器允许远程连接。
  • 禁用防病毒和防火墙尝试。
  • SQL Server 和 SQL Server Browser 服务都在运行。

关于什么可能导致问题或测试连接字符串的可靠方法的任何想法?

【问题讨论】:

  • 调试中,查看Model.CEREBROEntities.Database.Connection.ConnectionString的运行时值。
  • 这真的很有帮助,据我所见,它正在获取从事该项目的另一个人的连接字符串的值。

标签: sql-server visual-studio


【解决方案1】:

解决了。另一个层中有另一个 App.config 正在修改我正在使用的连接字符串。感谢 David Browne - Microsoft 指导我找到正确的答案。

【讨论】:

  • 在 2 天内,因为在那之前 SO 不会让我。
猜你喜欢
  • 2019-03-22
  • 2011-08-20
  • 1970-01-01
  • 2014-08-08
  • 2010-09-12
  • 2011-07-21
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多