title: sql_variant read using Entity Framework as an object cannot be cast to its actual type
body: <p>I have a large database that I use Entity Framework to connect to. One of these tables has a column of type <code>sql_variant</code>, which Entities doesn't support, so I read it using some raw SQL.</p>
  
  <pre><code>string MinWeight = CEntity.Database.SqlQuery&lt;string&gt;(MinQuery).FirstOrDefault();
  </code></pre>
  
  <p>Which worked fine.. until a data problem meant that 1% of the records we cared about stored a numeric in that sql_variant, and the above gave me a cast exception. I tested out this and it read the value in fine</p>
  
  <pre><code>double MinWeightdouble = CEntity.Database.SqlQuery&lt;double&gt;(MinQuery).FirstOrDefault();
  </code></pre>
  
  <p>But since I need to handle both double (SQL numeric) and string, I tried </p>
  
  <pre><code>object MinWeightVariant = CEntity.Database.SqlQuery&lt;object&gt;(MinQuery).FirstOrDefault();
  </code></pre>
  
  <p>Which I assumed I could easily cast to string or double, but that's not the case. Attempting to cast to a double gives me an error about how I can't cast an object to a double, <code>MinWeightVariant.GetType()</code> just gives me System.Object. It appears that, while it knows deep down in the sql read that it's numeric, that data gets lost when it's assigned to an object. Any idea how to fix this in a sensible way?</p>