title: EF Dynamic type - The SqlParameter is already contained by another SqlParameterCollection
body: <p>I have a stored procedure will will give a dynamic result set</p>
  
  <pre><code>(Eg1. storedProcedureNameXX 4 - sql server result  may be 5 columns)
  (Eg2. storedProcedureNameXX 1 - sql server result  may be 3 columns)
  </code></pre>
  
  <p>Lets assume i have added the dynamic columns in the Type Builder:</p>
  
  <pre><code>TypeBuilder builder = CreateTypeBuilder("MyDynamicAssembly", "MyModule", "MyType")&lt;br&gt;
  
  //Todo:get the dynamic column names
  
  CreateAutoImplementedProperty(builder, "column1", typeof(string));
  CreateAutoImplementedProperty(builder, "column2", typeof(string));
  CreateAutoImplementedProperty(builder, "column3", typeof(string));
  
  Type resultType = builder.CreateType();
  var parameters = new List&lt;SqlParameter&gt;();
  parameters.Add(new SqlParameter("parm1", 1));
  
  var  **p1** = entity.Database
      .SqlQuery(resultType, "exec storedProcedureNameXX @parm1", parameters.ToArray());
  </code></pre>
  
  <p>Error in P1 variable:</p>
  
  <blockquote>
    <p>"The SqlParameter is already contained by another
    SqlParameterCollection" Unable to place ToList()</p>
  </blockquote>
  
  <p>//Added methods used<br>
  private static TypeBuilder createTypeBuilder( string assemblyName, string moduleName, string typeName)
          {
              TypeBuilder typeBuilder = AppDomain
                  .CurrentDomain
                  .DefineDynamicAssembly(new AssemblyName(assemblyName),
                                         AssemblyBuilderAccess.Run)
                  .DefineDynamicModule(moduleName)
                  .DefineType(typeName, TypeAttributes.Public);
              typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
              return typeBuilder;
          }
  <br>
          private static void createAutoImplementedProperty(
              TypeBuilder builder, string propertyName, Type propertyType)
          {
              const string PrivateFieldPrefix = "m_";
              const string GetterPrefix = "get_";
              const string SetterPrefix = "set_";</p>
  
  <pre><code>        // Generate the field.
          FieldBuilder fieldBuilder = builder.DefineField(
              string.Concat(PrivateFieldPrefix, propertyName),
                            propertyType, FieldAttributes.Private);
  
          // Generate the property
          PropertyBuilder propertyBuilder = builder.DefineProperty(
              propertyName, System.Reflection.PropertyAttributes.HasDefault, propertyType, null);
  
          // Property getter and setter attributes.
          MethodAttributes propertyMethodAttributes =
              MethodAttributes.Public | MethodAttributes.SpecialName |
              MethodAttributes.HideBySig;
  
          // Define the getter method.
          MethodBuilder getterMethod = builder.DefineMethod(
              string.Concat(GetterPrefix, propertyName),
              propertyMethodAttributes, propertyType, Type.EmptyTypes);
  
          // Emit the IL code.
          // ldarg.0
          // ldfld,_field
          // ret
          ILGenerator getterILCode = getterMethod.GetILGenerator();
          getterILCode.Emit(OpCodes.Ldarg_0);
          getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);
          getterILCode.Emit(OpCodes.Ret);
  
          // Define the setter method.
          MethodBuilder setterMethod = builder.DefineMethod(
              string.Concat(SetterPrefix, propertyName),
              propertyMethodAttributes, null, new Type[] { propertyType });
  
          // Emit the IL code.
          // ldarg.0
          // ldarg.1
          // stfld,_field
          // ret
          ILGenerator setterILCode = setterMethod.GetILGenerator();
          setterILCode.Emit(OpCodes.Ldarg_0);
          setterILCode.Emit(OpCodes.Ldarg_1);
          setterILCode.Emit(OpCodes.Stfld, fieldBuilder);
          setterILCode.Emit(OpCodes.Ret);
  
          propertyBuilder.SetGetMethod(getterMethod);
          propertyBuilder.SetSetMethod(setterMethod);
      }    
  </code></pre>