为什么我会得到这个"无效的对象名称DBO *。'"误差MVC4? [英] Why am I getting this "Invalid object name 'dbo.*'" error with MVC4?

查看:172
本文介绍了为什么我会得到这个"无效的对象名称DBO *。'"误差MVC4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误:


  

无效的对象名称dbo.ImageMetas'。


在这一行:


  

返回查看(db.Images.ToList());


我在哪里数据库环境是这样的:

 公共类GalleryContext:的DbContext
{
    公共GalleryContext()
        :基地(DefaultConnection)
    {
    }    公共DbSet< ImageMeta>图片{搞定;组; }
    公共DbSet<&镜像文件GT;图像文件中获取{;组; }
}

和我的模型:

 公共类ImageMeta
{
    [键]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)
    公众诠释标识{搞定;组; }
    公众用户配置用户{搞定;组; }
    公共字符串名称{;组; }
    公共虚拟的ICollection<&镜像文件GT;文件{搞定;组; }
}

这是一个全新的项目MVC4。我写的第一个模型,然后自动生成的控制器。

检查数据库,实际上该表丢失。

我是IM pression,它会为我自动创建该表虽然下;为<一个href=\"http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller\"相对=nofollow>本教程建议。

那么,为什么是不是这样做?


好吧,如果我加入我的上下文命名的连接字符串(并取下底座的构造函数),现在它工作。为什么我不能用DefaultConnection,然后呢?

 &LT;添加名称=GalleryContext
   的connectionString =数据源=(的LocalDB)\\ V11.0; AttachDbFilename = | DataDirectory目录| \\ gallery.mdf;集成安全性=真
   的providerName =System.Data.SqlClient的
/&GT;


解决方案

我遇到过这个问题,并找到了解决办法,这需要在SQL用户架构是这样的表与DBO创建dbo架构中。在他们的开始,问题就解决了​​。

我是presuming这是由MS需要固定的错误。

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.

这篇关于为什么我会得到这个&QUOT;无效的对象名称DBO *。'&QUOT;误差MVC4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆