title: Entity framework, how to convert from proxy to object
body: <p>I'm loving EF so far. My project has started crashing when its trying to convert a System.Data.Entity.DynamicProxies.Badge to Badge. I don't know what to do. I've read the documentation on Proxies and I sort of understand it. I don't want to disable proxy functionality if it is beneficial to have it?</p>
  
  <p>How can I get around this? Basically I call a function MyFunc(myBadge); And because MyFunc takes a parameter of type Badge it crashes. Code is below:</p>
  
  <pre><code>foreach(Badge B in badgesOfBadgeType) {
  
  Assembly assembly = Assembly.LoadFile(GlobalHelper.GetCurrentContext().Server.MapPath("~") + "bin/MM.dll";
  Type classs = assembly.GetType("MM.Helpers.BadgeHelper");
  string methodName = "DoesUserEventUnlockBadge_"+B.ID;
  MethodInfo methodInfo = classs.GetMethod(methodName);
  
  if (methodInfo != null)
  {
      ParameterInfo[] parameters = methodInfo.GetParameters();
      object[] parametersArray = new object[] { B, UE };
      bool didUnlock = (bool)methodInfo.Invoke(null, parametersArray);
  
      if (didUnlock)
      {
          unlockedBadges.Add(B);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>calling func:</p>
  
  <pre><code>public static bool DoesUserEventUnlockBadge_1(Badge B, UserEvent UE)
          {
              //Check here..
              return true;
          }
  </code></pre>
  
  <p>New error, when I go db.Badges.find(b); and use that (I.E a non proxy)</p>
  
  <pre>
  System.ArgumentOutOfRangeException: The specified parameter type 'System.Data.Entity.DynamicProxies.Badge_90C17B99EFACD091308AD8F9030C6441D90D4237AC5630A4C2F673A79A186B27' is not valid. Only scalar types, such as System.Int32, System.Decimal, System.DateTime, and System.Guid, are supported.
  Parameter name: parameter
     at System.Data.Objects.ObjectParameterCollection.Add(ObjectParameter parameter)
     at System.Data.Objects.ObjectContext.CreateQuery[T](String queryString, ObjectParameter[] parameters)
     at System.Data.Entity.Internal.Linq.InternalSet`1.FindInStore(WrappedEntityKey key, String keyValuesParamName)
     at System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues)
     at System.Data.Entity.DbSet`1.Find(Object[] keyValues)
     at MusingMonkey.Helpers.BadgeHelper.UnlockedBadgesForEvent(BaseDBContext db, UserEvent UE, User user) in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Helpers\BadgeHelper.cs:line 31
     at MusingMonkey.Helpers.UserIdeaHelper.PushVoteToIdea(BaseDBContext db, Nullable`1 id, String direction) in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Helpers\UserIdeaHelper.cs:line 164
     at MusingMonkey.Controllers.IdeasController.Vote(Nullable`1 id, String direction) in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Controllers\IdeasController.cs:line 23
     at lambda_method(Closure , ControllerBase , Object[] )
     at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
     at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass42.&lt;BeginInvokeSynchronousActionMethod&gt;b__41()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass37.&lt;&gt;c__DisplayClass39.&lt;BeginInvokeActionMethodWithFilters&gt;b__33()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass4f.&lt;InvokeActionMethodFilterAsynchronously&gt;b__49()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass4f.&lt;InvokeActionMethodFilterAsynchronously&gt;b__49()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass37.&lt;BeginInvokeActionMethodWithFilters&gt;b__36(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass25.&lt;&gt;c__DisplayClass2a.&lt;BeginInvokeAction&gt;b__20()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass25.&lt;BeginInvokeAction&gt;b__22(IAsyncResult asyncResult)
  </pre>