title: Reference external type enum and contains method
body: <p>Since I have switched to .net 4.5 and EF 5.0 I have started using enums mapping.
  In my project I'm using model first. Because all of enums were declared earlier so I have decided to user option "Reference external type" while I was converting fields to enums in EDM. Everything works fine but when I'm trying to execute code like </p>
  
  <pre><code>public enum SomeEnum : int
  {
      value1 = 0,
      value2 = 1
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          TestDbEntities context = new TestDbEntities();
          var enumList = new List&lt;SomeEnum&gt;() { SomeEnum.value1, SomeEnum.value2 };
          var items = context.Table1.Where(e =&gt; enumList.Contains(e.EnumField));
          foreach (var item in items)
          {
              Console.Write(item.Id);
          }
          context.Dispose();
      }
  }
  </code></pre>
  
  <p>I'm getting ArgumentException with message:</p>
  
  <pre><code>The type 'SomeEnum' does not match the EDM enumeration type 'SomeEnum' or its underlying type 'Int32' Parameter name: value.    
  </code></pre>
  
  <p>What is wired is that when I'm not using reference external type everything is okay. I'm understand what is behind in EDM, but I cannot understand why this code is working when the enum is defined in model and later generated by T4</p>
  
  <pre><code>  &lt;EntityContainer Name="TestDbEntities" p1:LazyLoadingEnabled="true"&gt;
        &lt;EntitySet Name="Table1" EntityType="TestDbModel.Table1" /&gt;
      &lt;/EntityContainer&gt;
      &lt;EntityType Name="Table1"&gt;
        &lt;Key&gt;
          &lt;PropertyRef Name="Id" /&gt;
          &lt;PropertyRef Name="EnumField" /&gt;
        &lt;/Key&gt;
        &lt;Property Name="Id" Type="Int32" Nullable="false" /&gt;
        &lt;Property Name="EnumField" Type="TestDbModel.SomeEnum" Nullable="false" /&gt;
      &lt;/EntityType&gt;
      &lt;EnumType Name="SomeEnum" a:ExternalTypeName="ConsoleApplication1.SomeEnum" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" /&gt;
  </code></pre>
  
  <p>I would like to use reference external type but this is some limitation. I know that I can cast to underlying type, but this is a hack.</p>
  
  <p>----edit
  Here is a stack trace</p>
  
  <pre><code>at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateConstant(TypeUsage constantType, Object value)
  at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.&lt;&gt;c__DisplayClass88.&lt;TypedTranslate&gt;b__86(Expression e)
  at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
  at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate(IEnumerable`1 argument, String argumentName, Int32 expectedElementCount, Boolean allowEmpty, Func`3 map, Func`2 collect, Func`3 deriveName)
  at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate()
  at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.CreateExpressionList(IEnumerable`1 arguments, String argumentName, Boolean allowEmpty, Action`2 validationCallback)
  at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateNewCollection(IEnumerable`1 elements, DbExpressionList&amp; validElements)
  at System.Data.Common.CommandTrees.ExpressionBuilder.DbExpressionBuilder.CreateNewCollection(IEnumerable`1 elements)
  at System.Data.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.TypedTranslate(ExpressionConverter parent, NewArrayExpression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.ContainsTranslator.TranslateContains(ExpressionConverter parent, Expression sourceExpression, Expression valueExpression)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.ContainsTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
  at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding&amp; binding)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression&amp; source, DbExpressionBinding&amp; sourceBinding, DbExpression&amp; lambda)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
  at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  at System.Data.Objects.ELinq.ExpressionConverter.Convert()
  at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
  at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
  at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator()
  at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
  at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable&lt;TResult&gt;.GetEnumerator()
  at ConsoleApplication1.Program.Main(String[] args) in c:\Users\Pawel\Documents\Visual Studio 2012\Projects\ClassLibrary1\ConsoleApplication1\Program.cs:line 22
  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()
  </code></pre>