C# – Entity Framework Exception: Invalid object name

centity-frameworknet

I am trying to create database using Code First approach. When I run the following code I am getting the following exception. Is there anything wrong in the fields that I defined? How can we overcome this?

Exception:

An error occurred while updating the entries. See the inner exception for details.

Inner Exception:

"Invalid object name 'dbo.Dinners'.

Note: I do not have such a table (Dinners) in the database. The code is supposed to create the tables. I just gave connection string to identify the server as mentioned in EF Code First: Cannot connect to SQL Server. Should I change the connection string?

Connections String:

string connectionstring = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";

The LibraryReservationSystem database is already existing database. It has no tables. I am expecting EF to create the tables.

The connection string I copied from a working LINQ 2 SQL application. Do I need to make any changes to it to supply to EF?

enter image description here

UPDATE

When I included the following code, the exception got changed. Now it says – "Invalid object name 'dbo.Dinner'.". It is now complaining about Dinner table; not Dinners table.

    protected override void OnModelCreating(DbModelBuilder modelbuilder)
    {
        modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }

Original CODE

    static void Main(string[] args)
    {

        string connectionstring = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";

        using (var db = new NerdDinners(connectionstring))
        {
            var product = new Dinner { DinnerID = 1, Title = 101 };
            db.Dinners.Add(product);
            int recordsAffected = db.SaveChanges();
        }

    }


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 NerdDinners(string connString): base(connString)
    { 

    }

    public DbSet<Dinner> Dinners { get; set; }
    public DbSet<RSVP> RSVPs { get; set; }
}
}

REFERENCE

  1. http://nerddinner.codeplex.com/discussions/358197
  2. Entity framework – Invalid Object Name
  3. Invalid object name 'dbo.TableName' when retrieving data from generated table
  4. http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx

Best Answer

The LibraryReservationSystem database is already existing database. It has no tables. I am expecting EF to create the tables.

That's not correct. If the database exists EF doesn't create any tables in this database. EF can create the database if it doesn't exist. That is the default database initializer CreateDatabaseIfNotExists that gets applied if you don't change it explicitly. You can select two other initializers: DropCreateDatabaseAlways or DropCreateDatabaseIfModelChanges. But neither of those will only create tables in an existing database but instead delete the database completely and create it from scratch including all tables.

What can you do:

  • Either delete the database manually (in SSMS for example), then EF will create a new one including the tables
  • Or use the DropCreateDatabaseAlways initializer once to let EF create the database including the tables, then remove the initializer again
  • Or if you can't delete the database for whatever reason write SQL code in the Seed method that adds the tables to the database (Wrong, thanks to Mark Stafford's comment)
  • Or use Code-First Migrations (EF >= 4.3) to add new tables to an existing database when you have added new entities.