Why am I getting this "Invalid object name 'dbo.*'" error with MVC4?

asp.net-mvc-4 entity-framework visual-studio-2012


I get this error:

Invalid object name 'dbo.ImageMetas'.

On this line:

return View(db.Images.ToList());

Where my database context looks like this:

public class GalleryContext : DbContext
    public GalleryContext()
        : base("DefaultConnection")

    public DbSet<ImageMeta> Images { get; set; }
    public DbSet<ImageFile> ImageFiles { get; set; }

And my model:

public class ImageMeta
    public int Id { get; set; }
    public UserProfile User { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ImageFile> Files { get; set; } 

It's a brand new MVC4 project. I wrote the models first, then auto-generated the controller.

I inspected the database, and indeed the table is missing.

I was under the impression that it would auto-create the table for me though; as this tutorial suggests.

So why isn't it doing that?

Okay, if I add a connection string named after my context (and remove the base constructor), it works now. Why can't I use the DefaultConnection, then?

<add name="GalleryContext"
   connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\gallery.mdf;Integrated Security=True"
5/23/2017 12:04:47 PM

Popular Answer

I experienced this problem and found a solution, which requires that the User Schema in SQL is that of the dbo schema thus the tables are created with dbo. at the beginning of them, problem solved.

I am presuming this is a bug that will need fixed by MS.

8/22/2013 11:46:29 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow