Gamal Mohamed
Gamal Mohamed

Reputation: 149

Error when creating and connecting to Entity Framework database on an Azure virtual machine

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)

Upvotes: 0

Views: 808

Answers (1)

Amor
Amor

Reputation: 8491

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; }
}

Upvotes: 2

Related Questions