开发者

Entity Framework 4 ExecuteStoreQuery<T> -- unable to results to object-type property

开发者 https://www.devze.com 2023-02-18 01:02 出处:网络
I have an Entity that maps to a table EntityTable and is pretty straight forward, consisting of about 4 or 5 value type properties.Depending on the database I access, though, the schema is different f

I have an Entity that maps to a table EntityTable and is pretty straight forward, consisting of about 4 or 5 value type properties. Depending on the database I access, though, the schema is different for one of the properties: in most schemas, it is a decimal, in a handful of others, it is a smallint (or Int16 in C#). Obviously, this trips up EF when it tries to map an Int16 value into an Entity that expects a decimal-typed property.

So my work around was to call ExecuteStoreQuery() on the EF context and have FauxEntity declare the wonky property as an object and not explicitly decimal or Int16. I could then specifically handle the unboxing of the property manually.

However, this doesn't work! Instead of an object reference to a boxed Int16 (or decimal value), the property's value is null! This i开发者_开发百科s weird because I can easily declare something like:

object intVal = 16;

intVal will then become an instance of a boxed int value. I can then unbox appropriately.

This is what I'm hoping for with ExecuteStoredProcedure -- it should throw the value into the object property and then I can do a series of unboxing tests to properly unbox the value. However, the value is never successfully boxed in the object property!

Is this even possible with ExecuteStoreQuery<T>()? Why can't it load a value type into an object-typed property?

BTW, I'm aware I can solve the issue at hand by executing a DataReader against the SQL code and manually casting each result column. I'm simply wondering why ExecuteStoreQuery<T>() isn't behaving as anticipated.


The restrictions on TElement are in MSDN. Quoting:

For the previous methods that take a generic result type parameter, the TResult can be a primitive type, an entity type, or any custom type. The type does not have to be defined in the Entity Framework conceptual model. If the specified type is of a type that is not defined in the conceptual model or is not a primitive type, the following mapping conventions apply.

The type:

  • Must not be abstract.
  • Must have a default constructor.

Each property of the type:

  • Must have a setter.
  • Must correspond to a primitive type in CSDL.
  • Must correspond to a column name in the resulting DbDataReader (the provider implementation determines whether a column has the same name as the property). If the name of the type's property does not match a field of the DbDataReader, the Entity Framework materializes the default value of the property if it is defined in the conceptutal model.
0

精彩评论

暂无评论...
验证码 换一张
取 消