1

I'm running the test ASP.NET Core application and have added Microsoft.EntityFrameworkCore.SqlServer v2.2.6.

When I try to publish it to IIS, it works and there is no issue.

However, when I run "Start debugging" / Press F5 in Visual 2017 , the code throws an exception:

Instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

Here is my code :

public class mydb: DbContext 
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            string cnn = @"Server=testserver;Database=devdb;User Id=sa;Password=246800;";
            Debug.WriteLine("connection string :" + cnn);
            optionsBuilder.UseSqlServer(cnn);
        }
    }
}

public class AccountController : Controller 
{
    public ActionResult Login(SecurityForm objInput) 
    {
         using (mydb db = new mydb ()) {
              var _UserInfo = db.SystemLoginUser.FromSql("exec sp_checklogin @SLOGIN, @SPASSWORD", objInput.username, objInput.inputpasscode).ToList();
        } <-- Get server not found here when debug, but work in publish IIS
     }
}

and I would like this connection string write in the DbContext class.

Can anyone help with a solution for this issue?

Thank you

Michael
  • 61
  • 6
  • Where is your database located? If it is remote, make sure your database engine is configured to accept remote connections. Also you may need to add firewall exception. Check this question for more possible solutions: https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci – Nemanja Todorovic Jul 29 '19 at 12:46
  • Are the IIS and VS debugging on the same Computer? Try to connect `testserver` from VS SQL Server Object Explorer, will it be successfully connected? – Edward Jul 30 '19 at 06:39
  • My development laptop can connect SQL Server from my laptop via SQL Server 2014 management studio. and I have try to use MVC 5 project to the model, it also work in debugging mode. – Michael Aug 02 '19 at 02:37
  • Hi Tao Zhou , IIS and VS debugging should be same, as when I press F5 in VS 2017, the browser address will be http://localhost:50070/Account/Login?ReturnUrl=%2F – Michael Aug 02 '19 at 02:41

0 Answers0