title: Entity Framework ExecuteStoreQuery fails on deployed systems
body: <p>Ok, a bit stumped here, and instead of banging my head any more on this, I thought I'd toss this out to all the bright people here.  </p>
  
  <p>I'm using Entity Framework and c# for a WinApp solution, and the core of the problem is that the darned thing runs fine on my system, but after I deploy it, it fails.  Even when I install the deployed app on my development system, it fails.  I find that a bit disturbing.</p>
  
  <p>To the nuts and bold of the issue.  The user performs a search, I build a query, tap SQL via the ExecuteStoreQuery call and return a generic list of SearchResult objects.</p>
  
  <p>The call:</p>
  
  <pre><code>retVal = ctx.ExecuteStoreQuery&lt;SearchResults&gt;(sql).ToList();
  </code></pre>
  
  <p>Alternate call also generates the same error:</p>
  
  <pre><code>ObjectQuery&lt;DbDataRecord&gt; c = ctx.CreateQuery&lt;DbDataRecord&gt;(sql);
  </code></pre>
  
  <p>The Error:</p>
  
  <pre><code>************** Exception Text **************
  System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
     at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
     at System.Reflection.RuntimeModule.GetTypes()
     at System.Reflection.Assembly.GetTypes()
     at System.Data.Metadata.Edm.ObjectItemConventionAssemblyLoader.LoadTypesFromAssembly()
     at System.Data.Metadata.Edm.ObjectItemAssemblyLoader.Load()
     at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData)
     at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData)
     at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData)
     at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData)
     at System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage, Object&amp; loaderCookie, Dictionary`2&amp; typesInLoading, List`1&amp; errors)
     at System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
     at System.Data.Metadata.Edm.MetadataWorkspace.ImplicitLoadAssemblyForType(Type type, Assembly callingAssembly)
     at System.Data.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, MergeOption mergeOption, Object[] parameters)
     at AMMCred.Data.Search.GetSearchResults(SearchTerms terms)
     at AMMCred.frmMain.openProviderInfoToolStripMenuItem_Click(Object sender, EventArgs e)
     at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
     at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
     at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
     at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
     at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
     at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
     at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
     at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
     at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc(Message&amp; m)
     at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
     at System.Windows.Forms.ToolStrip.WndProc(Message&amp; m)
     at System.Windows.Forms.ToolStripDropDown.WndProc(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  </code></pre>
  
  <p>SearchResults is a custom object, like so:</p>
  
  <pre><code>public class SearchResults : IEquatable&lt;SearchResults&gt;
  {
      private Guid id = Guid.Empty;
      public Guid PROV_MPI_NO
      {
          get { return id; }
          set { id = value; }
      }
      private string iPA = "";
      public string IPA
      {
          get { return iPA; }
          set { iPA = value; }
      }
      private string firstName = "";
      public string FirstName
      {
          get { return firstName; }
          set { firstName = value; }
      }
      private string middleName = "";
      public string MI
      {
          get { return middleName; }
          set { middleName = value; }
      }
      private string lastName = "";
      public string LastName
      {
          get { return lastName; }
          set { lastName = value; }
      }
      private string license = "";
      public string License
      {
          get { return license; }
          set { license = value; }
      }
      private string provId = "";
      public string ProvId
      {
          get { return provId; }
          set { provId = value; }
      }
      /// &lt;summary&gt;
      /// Used to help identify duplicates in a collection
      /// &lt;/summary&gt;
      /// &lt;param name="other"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public bool Equals(SearchResults other)
      {
          return (this.PROV_MPI_NO == other.PROV_MPI_NO &amp;&amp; this.License == other.License);
      }
  }
  </code></pre>
  
  <p>And finally, here is the select part of the SQL being used to obtain the search results:</p>
  
  <pre><code>SELECT a.PROV_MPI_NO, b.COMPANY_ID AS IPA, a.FIRSTNAME,a.MI,a.LASTNAME, b.PROVID, c.LICENSE
  </code></pre>
  
  <p>The problem seems to be coming from within Entity Framework as it's binding the results of the SQL to the custom object.  I've verified that the custom object field names are the same as the SQL fields in case that mattered, which it didn't seem to.  </p>
  
  <p>It's darn near time for me to leave, and I'll be pondering this all weekend, so I'll probably check back in from time to time to answer any questions anyone might have.</p>
  
  <p>Like I said, I'm completely stumped at the moment and would totally appreciate anyone's insight.  </p>
  
  <p>Thanks in advance!</p>
  
  <p>Wayne</p>
  
  <p>UPDATE 11/14/2011----------
  Ran the Assembly Binding Log Viewer tool and came up with the following lead.  Looks like Microsoft.Practices.Unity isn't loading.  Thing is, I never once referenced these Practices assemblies in ANY application, but it seems that System.Data.Entity does...go figure.  It's a lead...any info on this new development is so very appreciated.</p>
  
  <pre><code>*** Assembly Binder Log Entry  (11/14/2011 @ 3:10:52 PM) ***
  
  The operation failed.
  Bind result: hr = 0x80070002. The system cannot find the file specified.
  
  Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
  Running under executable  C:\Users\***me***\AppData\Local\Apps\2.0\1ND1AZ7G.HEJ\CWDQKK8A.LVO\ammc..tion_0000000000000000_0002.0000_456d6de62e1c40e4\AMMCred2.exe
  --- A detailed error log follows. 
  
  === Pre-bind state information ===
  LOG: User = ***me***
  LOG: DisplayName = Microsoft.Practices.Unity, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
   (Fully-specified)
  LOG: Appbase = file:///C:/Users/***me***/AppData/Local/Apps/2.0/1ND1AZ7G.HEJ/CWDQKK8A.LVO/ammc..tion_0000000000000000_0002.0000_456d6de62e1c40e4/
  LOG: Initial PrivatePath = NULL
  LOG: Dynamic Base = NULL
  LOG: Cache Base = NULL
  LOG: AppName = AMMCred2.exe
  Calling assembly : Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\Users\***me***\AppData\Local\Apps\2.0\1ND1AZ7G.HEJ\CWDQKK8A.LVO\ammc..tion_0000000000000000_0002.0000_456d6de62e1c40e4\AMMCred2.exe.config
  LOG: Using host configuration file: 
  LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
  LOG: Post-policy reference: Microsoft.Practices.Unity, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
  LOG: GAC Lookup was unsuccessful.
  LOG: Attempting download of new URL file:///C:/Users/***me***/AppData/Local/Apps/2.0/1ND1AZ7G.HEJ/CWDQKK8A.LVO/ammc..tion_0000000000000000_0002.0000_456d6de62e1c40e4/Microsoft.Practices.Unity.DLL.
  LOG: Attempting download of new URL file:///C:/Users/***me***/AppData/Local/Apps/2.0/1ND1AZ7G.HEJ/CWDQKK8A.LVO/ammc..tion_0000000000000000_0002.0000_456d6de62e1c40e4/Microsoft.Practices.Unity/Microsoft.Practices.Unity.DLL.
  LOG: Attempting download of new URL file:///C:/Users/***me***/AppData/Local/Apps/2.0/1ND1AZ7G.HEJ/CWDQKK8A.LVO/ammc..tion_0000000000000000_0002.0000_456d6de62e1c40e4/Microsoft.Practices.Unity.EXE.
  LOG: Attempting download of new URL file:///C:/Users/***me***/AppData/Local/Apps/2.0/1ND1AZ7G.HEJ/CWDQKK8A.LVO/ammc..tion_0000000000000000_0002.0000_456d6de62e1c40e4/Microsoft.Practices.Unity/Microsoft.Practices.Unity.EXE.
  LOG: All probing URLs attempted and failed.
  </code></pre>