If you happen to be using configuration mapping (EntityTypeConfiguration
) classes to define your tables, you'll get this error if you forget to attach the Configuration class for the table to the Model Builder.
In my case, it really stumped me for a bit, because I already had another table (SomeThing
) working perfectly within this Context class. After simply adding a new table (OtherThing
) where everything seemed to be setup identical to the first, I got the error: Invalid object name 'dbo.OtherThings
.
The answer was in my Context class:
public DbSet<SomeThing> SomeThings { get; set; }
public DbSet<OtherThing> OtherThings { get; set; }
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new SomeThingMap());
// OOPS -- Forgot to add this!!
modelBuilder.Configurations.Add(new OtherThingMap());
}
For reference, here's my SomeThingMap
class:
public class SomeThingMap : EntityTypeConfiguration<SomeThing>
{
public SomeThingMap()
{
...
this.ToTable("SomeThing");
...
}
}
And my new OtherThingMap
class:
public class OtherThingMap : EntityTypeConfiguration<OtherThing>
{
public OtherThingMap()
{
...
this.ToTable("OtherThing");
...
}
}
It's a long shot, but I hope this helps point someone else in the right direction, at least.