treeblah
treeblah

Reputation: 1265

Invalid object name exception refers to an object that I haven't created

Currently, I am trying to pull information from a database using the EntityFramework. As such, I have connected to the proper database:

<add name="ChartConn" connectionString="Data Source=****;Initial Catalog=****;User ID=****;Password=****;" providerName="System.Data.SqlClient" />

where the asterisks are filled in with the correct personal information for that database.

The issue is when I attempt to pull information from this database with the following in my controller:

public ActionResult Index()
{
    var q = (from c in db.ChannelMaps
            select c.LABEL).ToList();

    return View(q);
}

and the following in the model, CHANNELMAP:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace ChartApp.Models
{
    public class CHANNELMAP
    {
        [Key]
        public int CHANNELNUM { get; set; }
        public int COLUMNID { get; set; }
        public string LABEL { get; set; }
        public string RAWUNITS { get; set; }
        public double? MULT { get; set; }
        public double? ADDER { get; set; }
        public int? DATATYPEID { get; set; }
        public int? OPCODEID { get; set; }
        public decimal? OPVALUE { get; set; }
        public int? SITEID { get; set; }
        public string DASNAME { get; set; }
    }

    public class ChannelMapContext : DbContext
    {
        public ChannelMapContext()
            : base("ChartConn")
        {
            Database.SetInitializer<Models.ChannelMapContext>(null);
        }

        public DbSet<CHANNELMAP> ChannelMaps { get; set; }

    }
}

I get an error: Invalid object name dbo.CHANNELMAPs, that points to the first line (from c in db.ChannelMaps) of my linq query in my controller. This is strange because my model's name is CHANNELMAP, so I am unsure as to why I am getting an error for an invalid name for an object that I have not used. What is causing this error? I have tried changing the name of my model numerous times, as well as triple-checking to make sure my connection string and property names are all set up accordingly.

Upvotes: 2

Views: 1846

Answers (2)

Rikard
Rikard

Reputation: 3869

By default EF adds an 's' (pluralization) at the end of each entities in the model. To remove that you just remove that convention:

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

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

This error means that EF is translating your LINQ into a sql statement that uses a table, which does not exist in the database.

Check your database and verify whether that table exists, or that you should be using a different table name.

Upvotes: 3

viggity
viggity

Reputation: 15227

Entity Framework assumes by convention that table names are pluralized versions of the entity name (hence the error message reference to CHANNELMAPs). If your entity is in fact called CHANNELMAP, add the Table attribute to it.

[Table("CHANNELMAP")]
public class CHANNELMAP
    { ...

see this for more info: Howto specify table name with Entity Framework Code First Fluent API

Upvotes: 1

Related Questions