#Summary
Sometimes with complicated table relations with enums as keys EntityFramework fails to load entities correctly.
It seems that lots of includes, like this
``` C#
var res = context.Orders
.Include(o => o.Prop1.PropertyState)
.Include(o => o.Prop2)
.Include(o => o.Prop3)
.ToList();
```
where each of the include contains a member of the enum type triggers the behavior.
See attached project for a complete minimalistic (hey, only 5 tables) example.
The above code snippet throws the following exception
```
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=The type of the key field 'PropertyStateId' is expected to be 'EfEnumBug.PropertyState', but the value provided is actually of type 'System.Int32'.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.EntityKey.ValidateTypeOfKeyValue(MetadataWorkspace workspace, EdmMember keyMember, Object keyValue, Boolean isArgumentException, String argumentName)
at System.Data.Entity.Core.EntityKey.ValidateEntityKey(MetadataWorkspace workspace, EntitySet entitySet, Boolean isArgumentException, String argumentName)
at System.Data.Entity.Core.EntityKey.ValidateEntityKey(MetadataWorkspace workspace, EntitySet entitySet)
at System.Data.Entity.Core.Objects.ObjectStateManager.CheckKeyMatchesEntity(IEntityWrapper wrappedEntity, EntityKey entityKey, EntitySet entitySetForType, Boolean forAttach)
at System.Data.Entity.Core.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MaterializeRow()
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MoveNext()
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.TryReadToNextElement()
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at EfEnumBug.Program.Main(String[] args) in c:\Users\albsun\Desktop\EfEnumBug\EfEnumBug\EfEnumBug\Program.cs:line 37
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
```
I expect it to return a list of orders with all properties set.
I have tried both EntityFramework 5.0.0 and EntityFramework 6.0.0-rc1 with the same result.
#Some observations
* Remove any of the .Include lines above and no exception is thrown
* Change Order.Prop1 from OrderProp1 to ICollection<OrderProp1> and no exception is thrown
* Change Order.Prop2 from ICollection<OrderProp2> to ICollection<OrderProp2> and no exception is thrown.