title: Enum as Key in entity framework 5 throwing error on many to many joins
body: <p>OK, this is a bit lengthy / obscure, but I'm getting an odd error in specific situation where I use an Enum as a table Key and attempt to query against the table while including more than one many-to-many related entities. </p>
  
  <p>The error, from the example code below is:</p>
  
  <pre><code>The type of the key field 'DietIs' is expected to be 'MvcApplication8.Models.DietIs', but the value provided is actually of type 'System.Int32'.
  </code></pre>
  
  <p>In a .net 4.5 web project, I have the following entity configuration :</p>
  
  <pre><code>public enum DietIs {
      None,
      Kosher,
      Paleo,
      Vegetarian
  }
  
  public class Diet {
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public DietIs DietIs { get; set; }
  
      public string Description { get; set; }
      public virtual ICollection&lt;Recipe&gt; Recipes { get; set; }
      public virtual ICollection&lt;Menu&gt; Menus { get; set; }
  }
  
  public class Recipe {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;Diet&gt; Diets { get; set; }
  }
  
  public class Menu {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;Diet&gt; Diets { get; set; }
  }
  
  public class EnumTestContextInit : DropCreateDatabaseAlways&lt;EnumTestContext&gt; {}
  
  public class EnumTestContext : DbContext {
      public DbSet&lt;Diet&gt; Diets { get; set; }
      public DbSet&lt;Menu&gt; Menus { get; set; }
      public DbSet&lt;Recipe&gt; Recipes { get; set; }
  
      public EnumTestContext() : base("EnumTestContext") {
          Configuration.LazyLoadingEnabled = false;
          Configuration.ProxyCreationEnabled = false;
      }
  }
  </code></pre>
  
  <p>In the Global.asax.cs file I initialize the database:</p>
  
  <pre><code> Database.SetInitializer(new EnumTestContextInit());
          using (var context = new EnumTestContext()) {
  
              var noDiet = new Diet { DietIs = DietIs.None, Description = "Whatever you want" };
              var paleoDiet = new Diet { DietIs = DietIs.Paleo, Description = "Like paleolithic peoples" };
              var vegDiet = new Diet { DietIs = DietIs.Vegetarian, Description = "No meat" };
  
              context.Menus.Add(new Menu { Name = "Cheese burger with Fries Menu", Diets = new List&lt;Diet&gt; { noDiet } });
              context.Menus.Add(new Menu { Name = "Mammoth Steak Tartar with Nuts Menu", Diets = new List&lt;Diet&gt; { paleoDiet, noDiet } });
              context.Menus.Add(new Menu { Name = "Soy Cheese Pizza Menu", Diets = new List&lt;Diet&gt; { vegDiet, noDiet } });
  
              context.Recipes.Add(new Recipe {Name = "Cheese burger", Diets = new List&lt;Diet&gt; {noDiet}});
              context.Recipes.Add(new Recipe { Name = "Mammoth Steak Tartar", Diets = new List&lt;Diet&gt; { paleoDiet, noDiet} });
              context.Recipes.Add(new Recipe { Name = "Cheese Pizza", Diets = new List&lt;Diet&gt; { vegDiet, noDiet } });
  
              context.SaveChanges();
          }
  </code></pre>
  
  <p>Then, I attempt to query against the database:</p>
  
  <pre><code>var context = new EnumTestContext();
  
          var dietsWithMenusAndRecipes = context.Diets
                    .Include(e =&gt; e.Menus)
                    .Include(e =&gt; e.Recipes)
                    .ToList();
  </code></pre>
  
  <p>Other queries where I use a single include load the expected data without issue. The query above, with two includes throws the error above. In the database I see autogenerated join tables (MenuDiets and RecipeDiets) and all the data looks correct. Again, as in the examples above I can query against the data but can't include multiple related entities without throwing the error. </p>
  
  <p>If I change the last query to only use a single include, I can load the other table without issue:</p>
  
  <pre><code>        var dietsWithMenusAndRecipes = context.Diets
                   .Include(e =&gt; e.Menus).ToList();
  
          foreach (var item in dietsWithMenusAndRecipes) {
              context.Entry(item).Collection(e =&gt; e.Recipes).Load();
              var rec = item.Recipes;
          }
  </code></pre>
  
  <p>Further — though this does not satisfy my use case as I want to restrict the table to just the enum values and unique constraints aren't supported in EF — this will work if I change the Diet entity class to use a separate identity key, rather than the Enum key:</p>
  
  <pre><code>    public int Id { get; set; }
      public DietIs DietIs { get; set; }
  </code></pre>
  
  <p>Another possible solution I explored was to explicitly create the join tables (MenuDiets and RecipeDiets) so that the join property key was typed as the Enum, but this still returned the above error.</p>
  
  <p>It really seems to be the multiple Includes that are causing it to choke. Any ideas as to whether I'm doing something wrong in the model setup? The query itself? A bug in Entity Framework?</p>