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
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
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"
providerName="System.Data.SqlClient"
/>
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.