title: ViewModel "no key defined" error
body: <p>I've recently been learning asp.net MVC 5 and EF6 and i've started to develop viewmodels for my views instead of stuffing values into the viewbag.</p>
  
  <p>I this is my viewmodel called <strong>EmployeeCreate</strong></p>
  
  <pre><code>namespace Rota.ViewModels
  {
      public class EmployeeCreate
      {
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public IEnumerable&lt;SelectListItem&gt; Departments { get; set; }
      }
  }
  </code></pre>
  
  <p>what i would like to do is on the create <code>[httpget] action method</code> to be able to populate a dropdown list</p>
  
  <p>this is my <strong>Employee</strong> enitity</p>
  
  <pre><code>namespace Rota.Models
  {
      public class Employee 
      {
          public int ID { get; set; }
  
          [Required]
          [Display(Name = "First Name")]
          public string FirstName { get; set; }
  
          [Required]
          [Display(Name = "Last Name")]
          public string LastName { get; set; }
  
          [ForeignKey("Department")]
          [DisplayFormat(NullDisplayText="No Department")]
          public int? DepartmentID { get; set; }
  
          public virtual Department Department { get; set; }
          public virtual ICollection&lt;Schedule&gt; Schedule { get; set; }
  
      }
  }
  </code></pre>
  
  <p>and this is my <strong>Department</strong> entity</p>
  
  <pre><code>    namespace Rota.Models
      {
          public class Department
          {
              [DatabaseGenerated(DatabaseGeneratedOption.None)]
              public int ID { get; set; }
  
              [Required]
              public string Name { get; set; }
  
              public virtual ICollection&lt;Employee&gt; Employees { get; set; }
          }
      }
  </code></pre>
  
  <p><strong>Edit</strong> this is my <strong>DbContext</strong> class</p>
  
  <pre><code>public class RotaContext : DbContext
      {
          public RotaContext() : base("RotaContext")
          {
  
          }
  
          public DbSet&lt;Department&gt; Departments { get; set; }
          public DbSet&lt;Shift&gt; Shifts { get; set; }
          public DbSet&lt;Employee&gt; Employees { get; set; }
          public DbSet&lt;Schedule&gt; Scheduled { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
          }
  
          public System.Data.Entity.DbSet&lt;Rota.ViewModels.EmployeeCreate&gt; CreateEmployeeViewModels { get; set; }
  
      }
  </code></pre>
  
  <p>this is my <strong>Create Action</strong> method in my controller</p>
  
  <pre><code>namespace Rota.Controllers
  {
      public class EmployeeController : Controller
      {
          //establish database connection
          private RotaContext db = new RotaContext();
  
          //GET: Employee/Create
          [HttpGet]
          public ActionResult Create()
          {
              EmployeeCreate ViewModel = new EmployeeCreate()
              {
                  Departments = db.Departments.Select(department =&gt; new SelectListItem {
                      Value = department.ID.ToString(),
                      Text = department.Name
                  })
              };
              return View(ViewModel);
          }
      }
  }
  </code></pre>
  
  <p>The trouble i've got is that i keep getting this error :</p>
  
  <blockquote>
    <p><strong>One or more validation errors were detected during model generation:</strong></p>
    
    <p>Rota.Entities.EmployeeCreate: : EntityType 'EmployeeCreate' has no key
    defined. Define the key for this EntityType. CreateEmployeeViewModels:
    EntityType: EntitySet 'CreateEmployeeViewModels' is based on type
    'EmployeeCreate' that has no keys defined.</p>
  </blockquote>
  
  <p>I have no idea why the application thinks my viewmodel is an entity, i can't see anything here that might suggest as such, to my knowledge, i thought i was populating an object with values from a database, and becuase the viewmodel has absolutely no association with the entities other than this, it doesn't need a key, because it doesn't need a unique identifier.</p>
  
  <p><strong>Full Stack Trace</strong></p>
  
  <blockquote>
    <p>[ModelValidationException: One or more validation errors were detected
    during model generation:</p>
    
    <p>Rota.Entities.EmployeeCreate: : EntityType 'EmployeeCreate' has no key
    defined. Define the key for this EntityType. CreateEmployeeViewModels:
    EntityType: EntitySet 'CreateEmployeeViewModels' is based on type
    'EmployeeCreate' that has no keys defined. ]<br>
    System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate() +338<br>
    System.Data.Entity.DbModelBuilder.Build(DbProviderManifest
    providerManifest, DbProviderInfo providerInfo) +370<br>
    System.Data.Entity.DbModelBuilder.Build(DbConnection
    providerConnection) +288<br>
    System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext
    internalContext) +94<br>
    System.Data.Entity.Internal.RetryLazy<code>2.GetValue(TInput input) +248<br>
    System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
    +543    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type
    entityType) +26<br>
    System.Data.Entity.Internal.Linq.InternalSet</code>1.Initialize() +72<br>
    System.Data.Entity.Internal.Linq.InternalSet<code>1.get_InternalContext()
    +21    System.Data.Entity.Infrastructure.DbQuery</code>1.System.Linq.IQueryable.get_Provider()
    +64    System.Linq.Queryable.Select(IQueryable<code>1 source, Expression</code>1 selector) +85    Rota.Controllers.EmployeeController.Create() in
    d:\Development\Rota\Rota\Controllers\EmployeeController.cs:87<br>
    lambda_method(Closure , ControllerBase , Object[] ) +79<br>
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
    controllerContext, IDictionary<code>2 parameters) +242<br>
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
    controllerContext, ActionDescriptor actionDescriptor, IDictionary</code>2
    parameters) +39<br>
    System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult
    asyncResult, ActionInvocation innerInvokeState) +12<br>
    System.Web.Mvc.Async.WrappedAsyncResult<code>2.CallEndDelegate(IAsyncResult
    asyncResult) +139<br>
    System.Web.Mvc.Async.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3d()
    +112    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass46.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f()
    +452    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass33.&lt;BeginInvokeActionMethodWithFilters&gt;b__32(IAsyncResult asyncResult) +15<br>
    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2b.&lt;BeginInvokeAction&gt;b__1c()
    +37    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult
    asyncResult) +241<br>
    System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult
    asyncResult, ExecuteCoreState innerState) +29<br>
    System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult
    asyncResult) +111<br>
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
    System.Web.Mvc.Async.WrappedAsyncVoid<code>1.CallEndDelegate(IAsyncResult
    asyncResult) +19<br>
    System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult
    asyncResult, ProcessRequestState innerState) +51<br>
    System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult
    asyncResult) +111<br>
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    +606    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +288</p>
  </blockquote>