【问题标题】:Invalid object name 'dbo.Infoes' when retrieving data from DB using EF使用 EF 从 DB 检索数据时对象名称“dbo.Infoes”无效
【发布时间】:2013-02-26 21:43:55
【问题描述】:

我真的是 MVC 的初学者。我制作的视图只是使用此代码从名为 Info 的表中检索数据

控制器:

namespace FP.WebUI.Controllers
{
    public class HomeController : Controller
    {
        private IRetrieveData repo;
        public HomeController(IRetrieveData repoParam)
        {
            repo = repoParam;
        }
        public ViewResult Index()
        {
            Info model = repo.Info.ToList().FirstOrDefault();
            return View(model);
        }

    }
}

还有观点:

<div id="follow">
    <a href="http://@Model.Facebook.Substring(Model.Facebook.IndexOf("http://")+1,Model.Facebook.Length)"><img src="img/temp.png" alt="facebook"/></a>
    <a href="http://@Model.Twitter.Substring(Model.Twitter.IndexOf("http://")+1,Model.Twitter.Length)"><img src="img/temp.png" alt="twitter"/></a>
    <a href="mailto://@Model.Email"><img src="img/temp.png" alt="email"/></a>
</div>

但我总是在一行中得到这个异常:

Info model = repo.Info.ToList().FirstOrDefault();

例外是:

“/”应用程序中的服务器错误。

无效的对象名称“dbo.Infoes”。

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.Data.SqlClient.SqlException:无效对象 名称“dbo.Infoes”。

我不知道问题出在哪里,所以我将把项目的相关部分。


Web.config内的连接字符串:

 <connectionStrings>    
    <add name="EFDbContext" providerName="system.data.sqlclient" connectionString="Data Source=.;Initial Catalog=photography;Integrated Security=True"/>
  </connectionStrings>

EFDbContext 类:

namespace FP.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<Gallery> Gallery { get; set; }
        public DbSet<SessionImages> SessionImages { get; set; }
        public DbSet<Sessions> Sessions { get; set; }
        public DbSet<Admins> Admins { get; set; }
        public DbSet<Info> Info { get; set; }
        public DbSet<Offers> Offers { get; set; }
    }
}

EFDbRetrieve 类:

namespace FP.Domain.Concrete
{
    public class EFDbRetrieve : IRetrieveData
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<Admins> Admins
        {
            get { return context.Admins; }
        }

        public IQueryable<Gallery> Gallery
        {
            get { return context.Gallery; }
        }

        public IQueryable<SessionImages> SessionImages
        {
            get { return context.SessionImages; }
        }

        public IQueryable<Sessions> Sessions
        {
            get { return context.Sessions; }
        }

        public IQueryable<Offers> Offers
        {
            get { return context.Offers; }
        }

        public IQueryable<Info> Info
        {
            get { return context.Info; }
        }
    }
}

IRetrieveData 接口:

namespace FP.Domain.Abstract
{
    public interface IRetrieveData : IAdmins, IGallery, ISessionImages, ISessions, IOffers, IInfo
    {
    }
}

继承自IInfo接口:

namespace FP.Domain.Abstract
{
    public interface IInfo
    {
        IQueryable<Info> Info { get; }
    }
}

如果有人在这方面帮助我,我将非常感激,我感到很沮丧。

【问题讨论】:

  • 这是一个映射错误(可能是由于从实体类创建的表名的复数形式),但您还没有包含表映射:)
  • @Trustme-I'maDoctor 你能给我提供一个教程或视频来解释那个映射的东西吗?非常感谢
  • Here's a tutorial with a video,包括映射。如果要自动映射,请使用答案中的解决方案。
  • 我想要恢复 3 小时的生命……感谢 Patryk 和 lopezbertoni(下)。我无法应用 modelBuilder.Conventions.Remove(); 的全局解决方案;所以我做了 modelBuilder.Entity().ToTable("UserEmailAddress");这巩固了命名约定。

标签: c# sql asp.net-mvc entity-framework sqlexception


【解决方案1】:

就像他们建议的那样,问题在于表名变得复数(Info 映射到名为 Infoes 的表)。您可以在 SQL 中检查表名。要禁用此功能,请添加:

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

到 EFDbContext 类。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 2020-04-08
    • 2015-07-28
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    相关资源
    最近更新 更多