title: Using multiple models in a single controller
body: <p>I have used EF Designer from database to build my models from a database created for my project.
  For a single controller, I need to reference multiple models.
  An example of this is that there is a Users table which contains the user department and office.
  I need to reference two separate tables DepartmentPermissions and OfficePermissions to determine what data the user is able to see.</p>
  
  <p>Here are examples of the auto generated models:</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated from a template.
  //
  //     Manual changes to this file may cause unexpected behavior in your application.
  //     Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace Project.Models
  {
      using System;
      using System.Collections.Generic;
  
      public partial class User
      {
          public int User_ID { get; set; }
          public string User_Username { get; set; }
          public string User_Department { get; set; }
          public string User_Office { get; set; }
      }
  }
  
  //------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated from a template.
  //
  //     Manual changes to this file may cause unexpected behavior in your application.
  //     Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace Project.Models
  {
      using System;
      using System.Collections.Generic;
  
      public partial class DepartmentPermission
      {
          public int DeptPerm_ID { get; set; }
          public string DeptPerm_Department { get; set; }
          public string DeptPerm_Role { get; set; }
      }
  }
  
  //------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated from a template.
  //
  //     Manual changes to this file may cause unexpected behavior in your application.
  //     Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace Project.Models
  {
      using System;
      using System.Collections.Generic;
  
      public partial class OfficePermission
      {
          public string OffPerm_OfficeID { get; set; }
      }
  }
  </code></pre>
  
  <p>The first column in each table is a primary key in the database.</p>
  
  <p>When creating a Controller using any of these autogenerated models all works fine, apart from I can only use one table.</p>
  
  <p>I want to be able to check the user's department and office from the Users table and then check the Role associated with the user's department from the DepartmentPermissions table and then check whether the user's office is in the OfficePermissions table.</p>
  
  <p>I read that I can build a View Model to create a new model which combines the models that I want to use so that I can get information from all relevant tables in the one controller.</p>
  
  <p>My example ViewModel is:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace Project.Models
  {
      public class HomeViewModel
      {            
          public IEnumerable&lt;User&gt; Users { get; set; }
          public IEnumerable&lt;OfficePermission&gt; OfficePermissions { get; set; }
          public IEnumerable&lt;DepartmentPermission&gt; DepartmentPermissions { get; set; } 
      }
  }
  </code></pre>
  
  <p>When I attempt to create a Controller with this view model, I get errors saying:</p>
  
  <p>User:: EntityType 'User' has no key defined. Define the key for this entity type.
  OfficePermission:: EntityType 'OfficePermission' has no key defined. Define the key for this entity type.
  DepartmentPermission:: EntityType 'DepartmentPermission' has no key defined. Define the key for this entity type.
  HomeViewModel:: EntityType 'HomeViewModel' has no key defined. Define the key for this entity type.
  Users: EntityType: EntitySet 'Users' is based on type 'User' that has no keys defined.
  OfficePermissions: EntityType: EntitySet 'OfficePermissions' is based on type 'OfficePermission' that has no keys defined. 
  DepartmentPermissions: EntityType: EntitySet 'DepartmentPermissions' is based on type 'DepartmentPermission' that has no keys defined. 
  HomeViewModels: EntityType: EntitySet 'HomeViewModels' is based on type 'HomeViewModel' that has no keys defined. </p>
  
  <p>I could define Keys manually in the autogenerated files, however this would be undone when the database is changed and the files require automatic regeneration.</p>
  
  <p>Is there a way to combine these autogenerated models in order to use them together in a controller?</p>