How do you convert numeric types in the mapping from a stored procedure to a complex type in Entity Framework 4?
We have two stored procedures in Microsoft SQL Server that return data that is conceptually of the same type. One returns an ID as SCOPE_TDENTITY() (a numeric), while the other returns 0 (an int32). We have a complex type with a property ID. If this property is a nullable decimal, we receive the following error when enumerating the results of the sproc that returns an int32:
The 'ID' property on 'EntitryRequestReturn' could not be set to a 'Int32' value. You must set this property to开发者_如何学Python a non-null value of type 'Decimal'.
Changing the type of the property to an int32 just moves the error to the other sproc, with the data types listed in the opposite order. Changing whether the types are nullable doesn't make any difference.
How can I get EF4 to convert an int column returned from a sproc into a decimal when constructing the complex type?
This worked in my instance. It may help here.
If the stored procedure returns data via a select statement, it helps greatly to cast all the return fields, even if they are in their native data types.
select convert(int, myInt) as myInt
works better than
select myInt
A lot of times my SPs would show a string as the last field when it was really an Int32. I added convert statements to all the fields and it never made that mistake again.
That said, you could simply do the conversion in the SP before it's sent to EF.
精彩评论