Vanishta Changea
Vanishta Changea

Reputation: 11

Update-Database - Error with (localdb)\MSSQLLocalDB

I am getting the following issue when running Update-Database after running Add-Migration:

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. Specified LocalDB instance name is invalid. )

Sql local DB:

enter image description here

In Startup.cs:

 // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        //services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration["ConnectionStrings = DefaultConnection"]));
        //var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;ConnectRetryCount=0";
        //var connection =    @"Data Source = (localdb)\\MSSQLLocalDB; Database = Ecommerce2DB; Integrated Security = True; Connect Timeout = 30; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False";
        var connection = @"Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = Ecommerce2DB; Integrated Security = True; Connect Timeout = 30; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False";
            //                      Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = master; Integrated Security = True; Connect Timeout = 30; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False
        services.AddDbContext<MyContext>(options => options.UseSqlServer(connection));

    }

Upvotes: 1

Views: 2569

Answers (2)

taril
taril

Reputation: 1

Unfortunately none of the above helped me. I solved it by just changing the Connection name.

{
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=(LocalDb)\\.;Initial Catalog=MyDbContext;Integrated Security=True;Pooling=False"
}
}

to

{
"AllowedHosts": "*",
"ConnectionStrings": {
"ApplicationContext": "Data Source=(LocalDb)\\.;Initial Catalog=MyDbContext;Integrated Security=True;Pooling=False"
}
}

Upvotes: 0

Irshad Ahmed Akhonzada
Irshad Ahmed Akhonzada

Reputation: 1418

This error message informs you that it is not possible to connect to MSSQL Server and app will not connect to the database. The possible reasons and the step for elimination are described below:

1) MSSQL Server is not started. Starting of it will allow you to see your MSSQL Server/instance in the drop-down list of available MSSQL Servers.

a) Go to the Start menu -> Control Panel -> Administration Tools -> Services.

b) In the list of services find SQL Server (instance name, by default it is .) and check its status, it must be Started (if it is not started, then right click on SQL Server and select Start from the context menu).

2) Firewall is blocking port 1433 (MSSQL standard port for connections). It can be disabled following the steps below:

a) Go to the Start menu -> Control Panel -> Administration Tools -> Services.

b) Find Firewall service, it must be disabled (if it is not, then right click the service and select Stop from the context menu).

Note: More information on this can be found on the official Microsoft site: http://msdn.microsoft.com/en-us/library/cc646023.aspx

3) TCP/IP protocol is disabled for MSSQL protocols. To enable it, see the steps below:

a) Navigate to SQL Server Configuration Manager in the Start menu.

b) Specify settings for TCP/IP protocol in SQL Server Configuration Manager.

c) Restart the computer.

Note: More information on this can be found on the official Microsoft site: http://msdn.microsoft.com/en-us/library/bb909712%28v=vs.90%29.aspx

4) Make sure your database engine is configured to accept remote connections (If you are using centralized database):

a) Open SQL Server Management Studio. b) Right click SQL Server instance -> Properties -> Connections -> Check the Allow remote connections to this server box. c) Go to the General section and check name of SQL Server specified in the Name field.

5) If you are using a named SQL Server instance, make sure you are using that instance name in your connection strings. Usually the format needed to specify the database server is machinename\instancename.

6) Make sure your login account has access permission on the database you used during login.

Alternative: If you still can’t get any connection, you may want to create a SQL account on the server, a corresponding SQL user on the database in question, and just use this username/password login data to connect to SQL Server.

Upvotes: 2

Related Questions