1

I am trying to create a database using code-first approach. When I am running the following code I am getting the following exception. How can we overcome this?

Notes:

  • I have SQL Server 2008 R2 on my system.
  • I am not using any config file. I assumed that it will create database using conventions
  • There is no open connection already; I just restarted the system and tested. Still the same issue.
  • Runtime version for EntityFramework.dll is v4.0.3xxx

Exception:

The provider did not return a ProviderManifestToken string

Message :

This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.

Inner Exception:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Code:

using System.Data.Entity;
namespace LijosEF
{
  public class Dinner
  {
      public int DinnerID { get; set; }
      public int Title { get; set; }
  }

  public class RSVP
  {
    public int RSVPID { get; set; }
    public int DinnerID { get; set; }

    public virtual Dinner Dinner { get; set; }
  }

  //System.Data.Entity.DbContext is from EntityFramework.dll
  public class NerdDinners : System.Data.Entity.DbContext
  {
    public DbSet<Dinner> Dinners { get; set; }
    public DbSet<RSVP> RSVPs { get; set; }
  }
}

namespace LijosEF
{
class Program
{
    static void Main(string[] args)
    {
        using (var db = new NerdDinners())
        {
            var product = new Dinner { DinnerID = 1, Title = 101 };
            db.Dinners.Add(product);
            int recordsAffected = db.SaveChanges();
        }

    }
}
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • I read from the comments below that any of the questions below answer the question asked. :) – Dennis Burton Jul 20 '12 at 14:25
  • @DennisBurton I won't be able to mark this question as answered untill http://stackoverflow.com/questions/11581147/ef-code-first-exception-invalid-object-name-dbo-dinners is answered. – LCJ Jul 20 '12 at 14:44

3 Answers3

5

Without any connection information. EF will try to create the database in SQL Express and not SQL Server. If you want it created in SQL Server you will need to provide a connection string.

Dennis Burton
  • 3,282
  • 3
  • 23
  • 30
2
public class NerdDinners : System.Data.Entity.DbContext
{
    public DbSet<Dinner> Dinners { get; set; }
    public DbSet<RSVP> RSVPs { get; set; }
}

change this to

public class NerdDinners : System.Data.Entity.DbContext
{
    public NerdDinners(string connString):base(connString)
    {}
    public DbSet<Dinner> Dinners { get; set; }
    public DbSet<RSVP> RSVPs { get; set; }
}
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
2

The easiest way to solve and know what you are doing is adding a Connectionstring in your app.config as following:

<connectionStrings>
    <clear/>
    <add name="NerdDinners"
          providerName="System.Data.SqlClient"
          connectionString="Server=.;Database=NerdDinners;Integrated Security=True;"/>
  </connectionStrings>
Wouter Janssens
  • 1,593
  • 10
  • 17