0

I created an virtual machine on Azure with VS2017 installed on it. Then, I tried to clone a project I'm working on that works on adding elements to a database using Entity Framework code-first.

But I get this error when trying to run the project:

Unhandled Exception: System.Data.SqlClient.SqlException: 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)

Any idea how to resolve this?

(N.B. the project is working fine on my local machine)

2
  • What are you trying to do? Deploy it to a remote machine or what? Commented May 14, 2017 at 21:30
  • @Mardoxx Setting-up a virtual machine to develop on it instead of my local machine.. Commented May 15, 2017 at 1:50

1 Answer 1

2

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

If your SQL Server instance is hosted on your local network, please make sure whether it has been configured that it can be remote access. If it can't be remote access, I am afraid you need to migrate your SQL Server database to your Virtual Machine or Azure SQL. Then you could modify your connection string to access the new database which can be accessed from your project.

I don't want to migrate anything, I want to make a new database on the VM..

You could use Local DB which works on your VM. If your application type is web application, you could modify your database as following. EF will create a new database in your App_Data folder.

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True

Otherwise, you need to put the detail configure the detail path of your database file in the connection string. For example,

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Database1.mdf;Integrated Security=True

I just tested the LocalDB on Azure Windows 10 virtual machine with VS 2017 installed and it can create database when I use EF code first. Following is my test code.

class Program
{
    static void Main(string[] args)
    {
        using (SchoolContext context = new SchoolContext())
        {
            context.Students.Add(new Student { ID = 1, FirstMidName = "FMN", LastName = "LN", EnrollmentDate = DateTime.Now });
            context.SaveChanges();
        }

        Console.Write("Create database OK");
        Console.Read();
    }
}

public class Student
{
    public int ID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public DateTime EnrollmentDate { get; set; }
}

public class SchoolContext : DbContext
{

    public SchoolContext() : base(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\amor\Desktop\TestEF\CodeFirstSample\Database1.mdf;Integrated Security=True")
    {
    }

    public DbSet<Student> Students { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't want to migrate anything, I want to make a new database on the VM..
I modified my reply based on your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.