I've just installed SQL Enterprise 2012 and now LocalDb appears to be inoperable. I can access LocalDb from the command line and see that the instance that I am referencing is indeed running, but when I run a project with Code First migrations enabled that uses the instance (localdb)\v11.0, I get an error stating that the connection string may be incorrect. I didn't change the connection string at all, it is the connection string that is set by default while creating a new project in Visual Studio 2012.
Has anyone had the same issue? Is there a fix for running both side by side?
Here's the exact error that I am getting:
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
And the exception:
System.Data.ProviderIncompatibleException: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct. ---> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: 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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.
I just realized what the issue was, hopefully this helps someone else.
Here is the section of my web.config that was causing errors:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
The parameter v12.0 value was the issue. It seems that after I installed SQL Server 2012, this generated parameter went from v11.0 to v12.0 causing a version conflict. I guess I could have installed the newest version of SQL Server Express (2014) and not had a problem at all since the version number would indeed be v12.0.
Related
I am trying to connect Visual Studio 2017 to SQL Server 2016 Express using the following code in my app.config:
<?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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<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>
<connectionStrings>
<add name="BlogDbContext" connectionString="Data Source=DESKTOP-I3K90LQ\SQL2016;Initial Catalog=CodeFirstDemo;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
I created a sample repository to test in my program.cs file
using System;
using System.Collections.Generic;
using System.Data.Entity;
sing System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Post
{
public int Id { get; set; }
public DateTime DatePublished { get; set; }
public DateTime DateEdited { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
public class BolgDbContext : DbContext
{
public DbSet<Post> Posts { get; set; }
}
class Program
{
static void Main(string[] args)
{
}
}
}
I installed Entity Framework into my Console Application using the following command in the Package Manager Console
PM>install-package EntityFramework -version 6.1.3
Then I enable Migration and create the first migration:
PM>enable-migrations
PM>add-migrations CreatePost
PM>update-database
after submitting update-database command I get following error message:
System.Data.SqlClient.SqlException (0x80131904): 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)
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1,State:0,Class:20
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)
So my Visual Studio is not able to connect to my SQL Server Management Studio to create a database.
If this work right, my Code First console project should be able to use a Sql instance instead of LocalDb instance to create and update a database from Visual Studio. ###
I have searched google for solutions for hours last night. Some suggest that could firewall issues, that I should create a port 1433 and enable TCP/IP in the Sql Server Configuration Manager to allow VS connecting to SQL. I have all those solutions, but none of them worked for my test project.
If anyone out there have an answer, please help.
Many thanks!
Further technical details
Target Framework: .NET Framework 4.5
Operating system:
IDE: (e.g. Visual Studio 2017 15.4)
Pretty sure by default EF looks for DefaultConnection connection name on your web.config. Someone can correct me if I'm wrong.
You need to override the connection name via :base() on your DbContext class.
public class BolgDbContext : DbContext
{
public BolgDbContext() : base("name=BlogDbContext") { }
public DbSet<Post> Posts { get; set; }
}
FYI, typo BolgDbContext is intentional, copied pasted from your code...
I found solution as Following:
<connectionStrings>
<add name="BlogDbContext" connectionString="Data Source=DESKTOP-I3K90LQ\SQL2016;Initial Catalog=CodeFirstDemo;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=localhost; Integrated Security=True; MultipleActiveResultSets=True"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
So glad I found this and your solution! I'm just posting here to describe how I got the same problem and a bit more detail on how to fix it.
The same in VS2019, with EntityFramework and Update-Database. The same error message, ie 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).
I was following a very good tutorial. It had connectionString="Data Source=.;Initial Catalog=FriendOrganizer;Integrated Security=True". This had been working for half of the tutorial, and Update-Database had created and seeded the DB, so I knew the DB was accessible.
After applying some changes in the tutorial it stopped working. I deleted the DB and Migrations, recreated the Migrations How to delete and recreate from scratch an existing EF Code First database, but it sill didn't work, with Update-Database producing this error, after a timeout.
Using the flag '-Verbose' assisted somewhat in tracking it down, but I needed this answer! I went to the Sql Server Object Explorer, right-clicked on the first '(localdb)\MSSQLLocalDB...', and got the Connection string property. I copied this and replaced it in the connectionStrings in the App.config for the data access project. Then Update-Database worked! (When I ran the app it timed out on the connection, as I needed to do the same in the Startup Project App.config, but that may not apply in all cases, depending on whether you have two App.configs).
For my db, FriendOrganizerDb the connection string is...
<connectionStrings>
<add name="FriendOrganizerDb"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=FriendOrganizer;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
providerName="System.Data.SqlClient"/>
</connectionStrings>
FriendOrganizer is the DB for the tutorial I was following - and highly recommend! (Building an Enterprise App with WPF, MVVM, and Entity Framework Code First)
Story :
I am trying to create an ASP.NET application hosted on Google Cloud.
This application needs database access to store some data and I am already using Entity Framework to access a local database.
So I am following this tutorial, hoping to find a way to host my database on Google Cloud :
https://cloud.google.com/dotnet/docs/getting-started/using-cloud-sql
But in the Configuring settings's part during the sixth step I get an error.
Configuration :
File : Web.config
<appSettings>
<add key="GoogleCloudSamples:ProjectId" value="area-185910" />
<add key="GoogleCloudSamples:BookStore" value="mysql" />
</appSettings>
<connectionStrings>
<add name="AreaDbEntities" connectionString="metadata=res://*/Models.AreaModel.csdl|res://*/Models.AreaModel.ssdl|res://*/Models.AreaModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\AreaDb.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="LocalMySqlServer" connectionString="Server=IP;Database=NAME;Uid=USERNAME;Pwd=USERPWD" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Error :
Visual studio Error
Website's page's error :
[SqlException (0x80131904): 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: 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.)]
Question :
Is someone able to explain how to make a database migration from a local EF database (with Entity Framework) to Google Cloud ?
Or is someone able to solve the error ?
Thank you in advance !
I've implemented Identity & OWIN authentication to my existing asp.net web form application. I referred to the site:
https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project
I am having problem with connection string for entity framework code first approach. I referred to the site: http://odetocode.com/Blogs/scott/archive/2012/08/14/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx for help. But none of them is working.
I use VS 2015, SQL Server 2014 & Windows 10 OS.
For SQL Server authentication (Windows authentication) my server name is : DESKTOP-07C2G55.
I changed my connection string as told in the sites but none of them worked. My first connection string is::
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=DESKTOP-07C2G55;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
On using this connection string when I try to register any user I am getting error as:
Directory lookup for the file "c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf" failed with the operating system error 5(Access is denied.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Directory lookup for the file "c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf" failed with the operating system error 5(Access is denied.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Then I run the following commands in my VS console. I get:
PM> Get-Service | Where-Object {$_.Name -like '*SQL*'}
Status Name DisplayName
------ ---- -----------
Running MSSQLFDLauncher SQL Full-text Filter Daemon Launche...
Running MSSQLSERVER SQL Server (MSSQLSERVER)
Running MSSQLServerOLAP... SQL Server Analysis Services (MSSQL...
Stopped SQLBrowser SQL Server Browser
Stopped SQLSERVERAGENT SQL Server Agent (MSSQLSERVER)
Running SQLWriter SQL Server VSS Writer
PM> SqlLocalDb info
MSSQLLocalDB
Then I changed my connection string, chaned the datasource to Data Source=MSSQLLocalDB:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=MSSQLLocalDB;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I get the error
Invalid value for key 'attachdbfilename'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Invalid value for key 'attachdbfilename'.
Again I changed my connection string to Data Source= .\SQLEXPRESS
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I got the 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)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: 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)
I can not figure out what is the problem whether the cause of the problem is connection string or any where else in the code. In other solutions, it is mentioned to edit DBContext Class, but in the tutorial there is no such class created. I am not using MVC, simple web form application trying to implement Identity and OWIN module. Please help me.
The output from your PowerShell script shows that you have a default instance (with the "instance" name of MSSQLSERVER) of SQL Server running:
Running MSSQLSERVER SQL Server (MSSQLSERVER)
This means, you can use Data Source=. (or Data Source=(local)) for your server name (you must not define an instance name to connect to the default instance), define the database name in Initial Catalog, and drop that AttachDbFileName crap once and for all (and let SQL Server handle all the file-related ins and outs - don't mess around with .mdf database files yourself!):
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.;Initial Catalog=WebFormsIdentity;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I have a load of unit tests that were running fine in Visual Studio 2013. I dont know if it is related, but I can't think of what else might have changed. I have moved my project to VS2015.
Now whenever I run my unit tests, all the tests that use a data context fail with an error indicating they are trying to use (localdb)\V11.0 and integrated security to access the database.
However, my development database is on .\sqlexpress and has a SQL login.
I've been through every config file, including the app.config in the unit test project, and they all specify the correct database. If I debug step through the code I can reach the line where the data context is created and inspect its properties and see the connection string is wrong, but I can't see where it is coming from.
Here's the relevant bit of my test project's app.config file:
<?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>
<connectionStrings>
<add name="speedEntitiesDev" connectionString="metadata=res://*/mydb.csdl|res://*/mydb.ssdl|res://*/mydb.msl;provider=System.Data.SqlClient;provider connection string="Server=.\sqlexpress;Database=thedatabase;User ID=theuser;Password=thepassword;Connection Timeout=30;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Server=.\sqlexpress;Database=thedatabase;User ID=theuser;Password=thepassword;Connection Timeout=30;MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
When I step into the unit test this is what the property inspector says is the value of the connection string is:
"Data Source=(localdb)\\v11.0;Initial Catalog=thedatabase;Integrated Security=True"
Note that the database name is correct, but everything else is wrong.
I did previously have a database hosted locally in localdb but after upgrading my development computer I stopped using it.
UPDATE:
I've discovered that if I rename the connection in the above file, then this breaks the test in a different way. Then I get an error that the connection string is not found:
System.InvalidOperationException: No connection string named 'speedEntitiesDev' could be found in the application config file..
Other changes to the connection string also have an effect (like if I change the database name, the connection goes to the new database name, and uses the login and password I supply.)
System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open database "thedatabase123" requested by the login. The login failed. Login failed for user 'theuser'..
When I set the connection string back to "thedatabase" it resets to using localdb and integrated security. It's like it's got an alias going or something.
After diving through the code for a few days, I noticed that in one older library referenced by every class tested by every failing test, there was a data update written as:
dc.SubmitChanges();
and it hit me, this class was using LINQ to Sql instead of Entity Framework. Somehow the class using LINQ to Sql was altering the connection string and defaulting to (localdb) which was what we were using when that library was written.
I changed the class to use Entity Framework instead and suddenly all my tests started respecting the connection string again.
This was weird because
The old referenced library didn't actually do any data updates any more (the data update code in it was legacy and long since placed elsewhere)
The old library didn't even have a valid reference to the data class that supplied its connection string
Despite this, just referencing the old library via the unit tests caused connection string weirdness.
I tried quite many various of the connection strings to get it working as follows:
<connectionStrings>
<add
name="CodeFirstTest"
providerName="System.Data.SqlClient"
connectionString="Server=ma\SQLEXPRESS;Initial Catalog=test;Integrated Security=true;Truested_Connection=true;"/>
</connectionStrings>
Exception message:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
I'm not sure the exact SQL Server I installed. But by SQL Server Management Studio I can see as properties :-
MA is the name of he pc windows 8.1 installed. Also I tried Server=.\SQLEXPRESS. I get same exception.
Then tried the following:
<connectionStrings>
<add name="Default"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=MA\LOCALDB#57E60DE5;Database=test;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
I get exception:
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).
Then modified data source=ma\SQLEXPRESS. Still get same error. How do I connect local db in .net / C#?
Change your connection string to be like below (notice the part Server=ma\\SQLEXPRESS;)
connectionString="Server=ma\\SQLEXPRESS;Initial Catalog=test;Integrated Security=true;Truested_Connection=true;"/>
Also notice the part in your connection string Truested_Connection=true;; it should be Trusted_Connection=True;. There is a spelling mistake.
Those keywords much match exactly else an exception would be thrown saying unknown keywords