0

I'm going straight to the point here.

I'm actually trying to learn C# asp.net mvc pattern here and I am using the Entity Framework code-first approach however when I tried to enter the update-database on the Package Manager Console it gives me error.

ClientConnectionId:00000000-0000-0000-0000-000000000000 Error Number:2,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: 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.)

it says that the server was not found or not accessible however, I've tried connecting on the sql server using the model first approach and there's nothing wrong with it... I don't know why it gives me error when I try the Entity Framework code-first approach.

I am using sqlserver 2014

I'm really new to this so please bear with me...

here's my connection string

  <connectionStrings>
     <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-movieRental-20170103091543.mdf;Initial Catalog=aspnet-movieRental-20170103091543;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

thanks in advance.

14
  • can you add connection string from web.config to your question. EF will use the connection string that has same name as your DBContext class. Commented Jan 4, 2017 at 7:07
  • hello @RaviA. I have added my connection string... my database instance is Q3Q1W1\SQLEXPRESS Commented Jan 4, 2017 at 7:16
  • Hi, i do not recommend creating connection string by yourself. Try to create it by using the function of IDE. The step is quite simple, for example VS2015 and a SQL-server 2014 database. In VS2015, click the folder of Model (default MVC folder), click Add, click New Item, in the left menu, select Data, then in the right panel select "ADO.NET Entity Data Model", select "Code First from database", then click New Connection to connect to your db, and select tables or store procedures for your MVC and EF to apply. You hard to go wrong with those tools Commented Jan 4, 2017 at 7:27
  • @SKLTFZ I was actually setting up the database on the sql server using the update-database.. it's like migrating my classes and make them into database. Commented Jan 4, 2017 at 7:30
  • i see, i think it is code-first approach, this simple tutorial might help. entityframeworktutorial.net/code-first/… . also you properly want to know more about code first migration later. Commented Jan 4, 2017 at 7:37

3 Answers 3

1

I happen to solve my problem by changing the instance of the connection string.

instead of localdb I change it to Q3A1W1\SQLEXPRESS which is an instance of my existing sql server.

<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=Q3A1W1\SQLEXPRESS;Initial Catalog=aspnet-movieRental-20170103091543;Integrated Security=True" />
Sign up to request clarification or add additional context in comments.

Comments

0

This error Shows when there is a problem in Connection String. Try to use following Connection String pattern.

@"Server=(localdb)\MSSQLLocalDB;Database=xyz;Trusted_Connection=True;Integrated Security=true;"

or

"Server=(localdb)\\MSSQLLocalDB;Database=xyz;Trusted_Connection=True;Integrated Security=true;"

Don't Forget to add Userid and Password if required in Connection String

Still your Problem persist then try following tweak.

  1. Check SQL Server service is running.
  2. Check for Remote Connection is enable.
  3. Check SQL Server default port 1433 Add in Firewall.
  4. Check TCP/IP is Enable in SQL Server Configure Manager-> protocol for SQLSERVER.

Comments

0

If you're on macOS 15.0 or later and you get

ClientConnectionId:00000000-0000-0000-0000-000000000000
[ERR] Failed to detected SqlServer version.
Microsoft.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: TCP Provider, error: 40 - Could not open a connection to SQL Server)

then you might have Settings > Privacy & Security > Local Network > [Your IDE/Text editor (Visual Studio Code in my case) set to 'off'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.