Search code examples
entity-frameworkentitydb-first

Entity framework with db mode cannot connect to server


I am trying to connect to by db instance using db first, I created a connection

 <add name="Entities" 
      connectionString="metadata=res://*/Models.ModelCmarket.csdl|res://*/Models.ModelCmarket.ssdl|res://*/Models.ModelCmarket.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v12.0;initial catalog=Cevaheer;integrated security=True;trustservercertificate=False;multisubnetfailover=True;MultipleActiveResultSets=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 
      providerName="System.Data.EntityClient" />

And I always get an 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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.)

But if i try

<add name="CevhermarketEntities" 
     connectionString="Data source=(localdb)\v12.0;initial catalog=Cevaheer;integrated security=True;trustservercertificate=False;multisubnetfailover=True;MultipleActiveResultSets=True;user id=dbuser;password=flexsin@123!;multipleactiveresultsets=True;"
     providerName="System.Data.SqlClient" /> 

I can connect, and also can connect from VS sql server explorer and SSMS.


Solution

  • To begin - there are 4 issues that could be causing the common LocalDb SqlExpress Sql Server connectivity errors SQL Network Interfaces, error: 50 - Local Database Runtime error occurred, before you begin you need to rename the v11 or v12 to (localdb)\mssqllocaldb

    I found that the simplest is to do the below - I have attached the pics and steps for help.

    First verify which instance you have installed, you can do this by checking the registry and by running cmd

    1. cmd> Sqllocaldb.exe i
    2. cmd> Sqllocaldb.exe s "whicheverVersionYouWantFromListBefore" if this step fails, you can delete with option d cmd> Sqllocaldb.exe d "someDb"
    3. cmd> Sqllocaldb.exe c "createSomeNewDbIfyouWantDb" create - the pic is error
    4. cmd> Sqllocaldb.exe start "createSomeNewDbIfyouWantDb"

    SqlLOCALDb_edited.png