Cascascap
Cascascap

Reputation: 49

Connection problem with SQL Server using Entity Framework in Visual Studio

Error:

A 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: 26 - Error Locating Server/Instance Specified)

Here's the function that throws the error

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;
        }
}

The connection string was automatically generated with an ADO.NET Entity DataModel which uses a working DB connection (pressing the "Test Connection" button gives positive feedback). I gave the model the name CEREBRO and called the connection string 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>

I can also connect to the server using Microsoft SQL Server Management Studio with windows authentication and SQL Server authentication, but none of them worked on Visual Studio.

Using the command line sqlcmd -L lists the server

Things i tried:

Any ideas on what could be causing the problem or reliable ways to test a connection string?

Upvotes: 0

Views: 1638

Answers (1)

Cascascap
Cascascap

Reputation: 49

It's solved. There was another App.config in another layer which was modifying the connection string i was using.Thank you to David Browne - Microsoft for guiding me to the right answer.

Upvotes: 1

Related Questions