0
votes

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.

2
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. - Ravi A.
hello @RaviA. I have added my connection string... my database instance is Q3Q1W1\SQLEXPRESS - Sam Teng Wong
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 - SKLTFZ
@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. - Sam Teng Wong
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. - SKLTFZ

2 Answers

1
votes

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" />
0
votes

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.