Ke Vin
Ke Vin

Reputation: 3760

How to connect to SQL Server 2008 with asp net core 2?

i am learning a asp.net core 2, i am new at microsoft technology which is using Entity Framework and using Package Manager Console, what i do here is i am following this tutorial https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model

which is teach me about connection to database, but there is just connection to localdb, which is provided like this :

appsettings.json :

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "MovieContext": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

but i want to connect to my SQL Server 2008 R2 which i installed it on my local server

here is my connection string look like :

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "MovieContext": "Server=localhost\\SQLEXPRESS;Database=Movie-1;MultipleActiveResultSets=true"
  }
}

then i run a command "Update-Database" in Package Manager Console, then i got this 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: 26 - Error Locating Server/Instance Specified)

but i can connect to my Sql server 2008 with SQL Studio management perfectly, which is i just put the servername : localhost and the default login. What do i miss there?

Upvotes: 0

Views: 3764

Answers (1)

Ke Vin
Ke Vin

Reputation: 3760

I just found out that actually i don't need instance for the connection string, so here is how my appsettings.json looks like

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "MovieContext": "Server=localhost;Database=Movie-1;User Id=root;Password=12345;MultipleActiveResultSets=true"
  }
}

Upvotes: 1

Related Questions