title: MVC 5, EF 6 Barebones Invalid Column Name 'Model'
body: <p>After fighting this in a project mixing webforms and MVC, I have now started an empty project and still see "Invalid Column Name 'Model'. error.  The barebones test project is as follows:</p>
  
  <p>VS2012 -> New Project -> ASP.NET MVC 5 Empty Project (Visual C#), named it testef</p>
  
  <p>My Data Context <strong>NosContext.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  
  namespace testef.Domain_Models
  {
      public class NosContext : DbContext
      {
          public NosContext() : base("Name=NosContext")
          {
  
          }
  
          public DbSet&lt;Nos_Templates&gt; Templates { get; set; }
      }
  }
  </code></pre>
  
  <p>My only model, <strong>Nos_Templates.cs</strong></p>
  
  <pre><code>using System;
  using System.ComponentModel.DataAnnotations;
  
  namespace testef.Domain_Models
  {
      public class Nos_Templates
      {
          [Key]
          public Guid id { get; set; }
          public string template_name { get; set; }
          public string edit_region { get; set; }
          public string question_list { get; set; }
          public DateTime? create_date { get; set; }
          public Guid? creating_user { get; set; }
          public DateTime? update_date { get; set; }
          public Guid? updating_user { get; set; }
      }
  }
  </code></pre>
  
  <p>The index method of controller <strong>HomeController.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Net;
  using System.Web;
  using System.Web.Mvc;
  using testef.Domain_Models;
  
  namespace testef.Controllers
  {
      public class HomeController : Controller
      {
          private NosContext db = new NosContext();
  
          // GET: /Home/
          public ActionResult Index()
          {
              return View(db.Templates.ToList());
          }
  
          protected override void Dispose(bool disposing)
          {
              if (disposing)
              {
                  db.Dispose();
              }
              base.Dispose(disposing);
          }
      }
  }
  </code></pre>
  
  <p>And connection string in <strong>web.config</strong></p>
  
  <pre><code> &lt;connectionStrings&gt;
      &lt;add name="NosContext" connectionString="Data Source=localhost;Initial Catalog=testdb;Integrated Security=True" providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>And finally the structure of the table in the DB</p>
  
  <pre><code>id               uniqueidentifier   NotNull
  template_name    varchar(150)       Nullable
  edit_region      ntext              Nullable
  question_list    ntext              Nullable
  create_date      datetime           Nullable
  creating_user    uniqueidentifier   Nullable
  update_date      datetime           Nullable
  updating_user    unique identifier  Nullable
  </code></pre>
  
  <p>When the controller executes <strong>return View(db.Templates.ToList());</strong> it throws the exception -</p>
  
  <p>"Invalid column name 'Model'.</p>
  
  <p>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. </p>
  
  <p>Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'Model'."</p>
  
  <p>I can take the generated SQL code (as grabbed from within the VS exception handler) and run against the DB in Sql manager studio and it works just fine.</p>
  
  <p>I don't understand the error as it acts like I've specified a column in the model that does not exist in the database table or have something in the table that does not exist in the model.  I have tried creating by hand and by reverse engineering code-first and same error no matter.</p>
  
  <p>Any ideas why it's throwing an error on non-existent column like this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I did a generate script to script the table to new query window and created a new DB called testEF and ran the create script to generate the table in question.  This new DB works just fine without the error. This <strong>does not</strong>  explain why it doesn't work on the existing db/table - even when running said script to drop/create the table in the DB that is not working.  So it appears something is amiss in SQL and not the code.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>There were a couple items out of skew </p>
  
  <ul>
  <li>The original DB creator did not define any primary keys, so it didn't play nice and EF had a hard time I guess due to this. </li>
  <li>Using the Code First tools was not generating a proper connection string (I did not change anything, but I am now getting this properly) H/T to @Uber Bot for this.</li>
  </ul>
  
  <p>After defining proper keys and re-running the code first existing db EF tools, I have proper model and connection strings and it works, both in new solution and existing solution where it initial was not working.</p>
  
  <p>Thanks for all the insight and tips.</p>