2

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.

treeblah
  • 1,205
  • 2
  • 11
  • 26
  • What DB structure do you have? – Vsevolod Goloviznin Aug 17 '15 at 19:15
  • Pretty sure that `public DbSet ChannelMaps { get; set; }` should be `public DbSet CHANNELMAPs { get; set; }` in your `ChannelMapContext` class. I believe (if I remember right) that it takes the type name and appends a pluralizer to it for mapping. – Ron Beyer Aug 17 '15 at 19:16
  • I tried changing around how I declared the DBSet variable, but it ended up not making a difference. The selected answer fixed everything without needing to change my DBSet variable. – treeblah Aug 17 '15 at 20:09

2 Answers2

3

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.

Rikard
  • 3,828
  • 1
  • 24
  • 39
  • Both of these answers fix the problem completely, but I will choose this one because it gives a better idea of what is going on behind the scenes. Thanks for the help! I was tearing my hair out for two hours trying to debug this, and the fix ended up being so simple. – treeblah Aug 17 '15 at 20:03
1

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

Community
  • 1
  • 1
viggity
  • 15,039
  • 7
  • 88
  • 96