Search code examples
c#sqlasp.net-mvcentity-frameworksqlexception

Invalid object name 'dbo.Infoes' when retrieving data from DB using EF


I'm really a beginner to MVC. I've made view that simply retrieves data from a table named Info using this code

Controller :

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);
        }

    }
}

And the view :

<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>

But i always get this exception right in the line :

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

The exception is :

Server Error in '/' Application.

Invalid object name 'dbo.Infoes'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Infoes'.

I have no idea where's the problem so i'll put the related parts of the project.


The connection string inside the root Web.config:

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

EFDbContext class :

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 class :

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 interface :

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

Which inherits from IInfo interface :

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

I'd be very grateful if someone helped me out in this, i'm getting frustrated.


Solution

  • Like they suggested, the problem is that the table names are getting pluralized (Info maps to a table called Infoes). You can check the table names in SQL. To disable this, add:

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

    to the EFDbContext class.

    Hope this helps