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.

Sam Teng Wong
  • 2,379
  • 5
  • 34
  • 56
  • 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. Jan 04 '17 at 07:07
  • hello @RaviA. I have added my connection string... my database instance is `Q3Q1W1\SQLEXPRESS` – Sam Teng Wong Jan 04 '17 at 07: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 – SKLTFZ Jan 04 '17 at 07: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. – Sam Teng Wong Jan 04 '17 at 07:30
  • i see, i think it is code-first approach, this simple tutorial might help. http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx . also you properly want to know more about code first migration later. – SKLTFZ Jan 04 '17 at 07:37
  • @SKLTFZ if I am right after I execute the `update-database` .mdf file named `aspnet-movieRental-20170103091543` should be added on my app_data.... but I don't know why it gives me this error though.. it is weird.. – Sam Teng Wong Jan 04 '17 at 07:41
  • i never use sql express for EF6, however as i know SQLexpress properly need some addons installed in order to run next level db infrastructure correctly. if you really didn't install given tools, it worth to give it a try?http://stackoverflow.com/questions/32830336/how-to-add-localdb-to-visual-studio-2015-communitys-sql-server-object-explorer – SKLTFZ Jan 04 '17 at 07:50
  • @SKLTFZ it seems I don't have a localdb instance on my sql server... so what I did is I change the connection string to this and it working now. `` – Sam Teng Wong Jan 04 '17 at 08:10
  • first delete migrate.cs and follow this steps http://stackoverflow.com/questions/17922945/how-to-enable-migration-to-update-my-database-in-mvc4/30957989#30957989 – Pathik Tailor Jan 04 '17 at 08:46
  • @Pathik yes this is what I did... but my problem is the connection since it cannot find the localdb instance... – Sam Teng Wong Jan 04 '17 at 08:57
  • @SamTengWong Did changing the connection string helped ? – Ravi A. Jan 04 '17 at 11:09
  • @RaviA. yes I did change the connection string and it work fine now. – Sam Teng Wong Jan 05 '17 at 00:25
  • @SamTengWong That's great. Would be great if you can add it as answer and accept it for future help. – Ravi A. Jan 05 '17 at 03:33
  • @RaviA. sure I'll update it later.. – Sam Teng Wong Jan 05 '17 at 04:15

2 Answers2

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" />
Sam Teng Wong
  • 2,379
  • 5
  • 34
  • 56
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.
Arjun Singh
  • 125
  • 9