Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First Error "Error Locating Server/Instance Specified"

I'm trying to use Code First with my local instance of Sql Server 2008 R2. I've create a user 'dev' and can log in and create databases using Sql Managment Studio. The problem is I keep getting an error message when trying to create a database using DbContext in EntityFramework. Here is the error message:

"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. SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified"

The error message I checked my Sql Server and it does allow remote connections.

I've abstracted my system with the following code and get the same error:

namespace CodeFirstConsole
{
    public class Program
    {
        static void Main(string[] args)
        {
            var db = new MyContext();
            try { Console.WriteLine(db.Parents.Count()); }
            catch (Exception) { throw; }
            Console.Read();
        }
    }

    internal class MyContext : DbContext
    {
        public DbSet<ParentObject> Parents { get; set; }
        public DbSet<ChildObject> Children { get; set; }

        public MyContext()
        {
            this.Database.Connection.ConnectionString =
                "Data Source=.;Database=ConsoleTest;Initial Catalog=ConsoleTest;User ID=dev;Password=dev;";
        }
    }

    internal class ParentObject
    {
        public int Id { get; set; }
        public string PropertyOne { get; set; }
    }

    internal class ChildObject
    {
        public int Id { get; set; }
        public bool PropertyOne { get; set; }
        public string PropertyTwo { get; set; }

        public virtual ParentObject Parent { get; set; }
    }

    internal class MyInitializer : DropCreateDatabaseAlways<MyContext>
    {

        protected override void Seed(MyContext context)
        {
            context.Parents.Add(new ParentObject() { PropertyOne = "hi" });
            base.Seed(context);
        }
    }
}
like image 347
Andrew Boes Avatar asked Jun 18 '12 21:06

Andrew Boes


People also ask

How do I fix Error Locating server instance specified?

Make sure your server name is correct, e.g., no typo on the name. When you try to connect to an SQL Server instance on another server, make sure the server machine is reachable, e.g, DNS can be resolve correctly, you are able to ping the server (not always true). Make sure SQL Browser service is running on the server.

What is error 26 in SQL Server?

SQL Server Error 26 occurs when users trying to connect to a SQL Server named instance. The reason of the SQL Server Error 26 is the client stack could not receive SSRP response UPD packet from SQL Browser.


1 Answers

I had the same error that drove me nuts for about a day. My situation was I had a large solution with a pre-existing start-up project and I was adding EF to the persistence project.

So the first step was to add the EF dependency. This created an app.config in my persistence project with the correct EF content. Then I went to Enable-Migrations and got the same error in this post. At this point I didn't think of copying the EF app.config settings to the app.config of the start-up project since I thought I'd have to play around with it before I would eventually run the app.

The problem was resolved when I changed the solution start-up project to the persistence project so I could get EF to find the correct app.config. Or I could have copied the EntityFramwework related section to the app.config of the start-up project.

like image 105
Christian Maslen Avatar answered Sep 23 '22 14:09

Christian Maslen