title: How do I use EF6 with Database First and existing views?
body: <p>I'm an EF noob (any version) and my Google-foo has failed me on finding out how to do this. Which makes me think I must be doing this wrong, but here is the situation:</p>
  
  <p>I'm definitely in an environment that is database first and the schema won't be updated by us coders. I'm also not a fan of 'automatic' code generation, so I've stayed away from the designer or the EF powertools (though I did run through them just to see them work).</p>
  
  <p>To learn I imported the Northwind DB into my LocalDB to have something to play with while creating some simple Web API 2 endpoints. This all went well as I created slimmed down models of the Employees, Shippers, &amp; Region tables in Northwind. Region was particularly interesting as it wasn't plural and EF had issues with that. Anyway, I got by that.</p>
  
  <p>My trouble now is; I want to use a view instead of a table as my source and whatever I'm doing just doesn't seem to work. What I tried was setting it up just like I did the tables. But that produces a <code>ModelValidationException</code> error. I tried looking at the auto-generated code from the designer, but got no insight.</p>
  
  <p><strong>My models:</strong></p>
  
  <pre><code>//-- employee, shipper, &amp; region work as expected
  public class employee {
      public int EmployeeID { get; set; }
      public string LastName { get; set; }
      public string FirstName { get; set; }
  }
  
  public class shipper {
      public int ShipperID { get; set; }
      public string CompanyName { get; set; }
      public string Phone { get; set; }
  }
  
  public class region {
      public int RegionID { get; set; }
      public string RegionDescription { get; set; }
  }
  
  //-- invoice is a view (actual viewname is 'Invoices')
  //-- so i followed the same rules as i did for employee &amp; shipper
  //-- i have tried uppercase 'I' as well as a plural version of the model
  public class invoice {
      public string CustomerID { get; set; }
      public string CustomerName { get; set; }
      public string Salesperson { get; set; }
      public int OrderID { get; set; }
      public int ProductID { get; set; }
      public string ProductName { get; set; }
  }
  </code></pre>
  
  <p><strong>My Context looks like this:</strong></p>
  
  <pre><code>public class NorthwindDBContext : DbContext {
      public DbSet&lt;Employee&gt; Employees { get; set; }
      public DbSet&lt;shipper&gt; Shippers { get; set; }
      public DbSet&lt;region&gt; Regions { get; set; }
      public DbSet&lt;Invoice&gt; Invoices { get; set; } //-- offending line of code
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder) {
          //--- fix for Region being singular instead of plural
          modelBuilder.Entity&lt;region&gt;().ToTable("Region");
      }
  }
  </code></pre>
  
  <p>If I comment out the <code>public DbSet&lt;Invoice&gt; Invoices { get; set; }</code> line in the context everything works. Just by having the line present (even if i don't reference the Invoices property) I receive the <code>ModelValidationException</code> error when using the context in anyway.</p>
  
  <p>Can anybody tell me what I'm doing wrong here?
  Thanks.</p>
  
  <p>Update: I tried this in one of my controllers, but I am too noob'ish to know if this is the right path either, though it worked as far as getting records.</p>
  
  <pre><code>using (var dbContext = new NorthwindDBContext()) {
      return dbContext.Database.SqlQuery&lt;Invoice&gt;("select * from invoices").ToList();
  }
  </code></pre>