0

This happens when mapping a stored procedure that pulls data from a table that defines one the fields as 'float'

If I ignore this issue, I end up with the following error:

The 'FieldName' property on 'EOST_GetSome_Result' could not be set to a 'System.Double' value. You must set this property to a non-null value of type 'System.Int32'.

Mapped field:

   public Nullable<int> FieldName { get; set; }

Changing float to int at the database level is not an option, so how to handle this situation at the application level?

usefulBee
  • 9,250
  • 10
  • 51
  • 89
  • 1
    Can't you just change the type of the variable to Nullable? BTW, I'd also recommend using the more common syntax of int? or float? – Jeff Siver Aug 21 '15 at 21:14
  • @Jeff, I could change it but " This code was generated from a template" and it could easily be overridden if the model is updated anytime; which is unreliable way to do it. – usefulBee Aug 21 '15 at 21:31
  • 1
    If you can't change the data type of the stored procedure and you can't change the data type of the model, the possibilities are not clean. Your best bet is to change one of those two things. – Jeff Siver Aug 21 '15 at 21:43
  • Thanks Jeff. Because of this limitation and still do not know why EntityFramework did not map this at least as double instead of integer, I had to rely on a traditional way of calling stored proc as described in here http://stackoverflow.com/questions/1260952/how-to-execute-a-stored-procedure-within-c-sharp-program – usefulBee Aug 26 '15 at 17:09

1 Answers1

0

I realize this is five years later, but I just ran into exactly this situation yesterday, or at least this error message. Here's what I discovered.

I have a view that combines the results of two other views. The C# model that maps to this view clearly says that my property is an integer, and the "union" view as we'll call it, shows that the column type is an "int" in SSMS. However, the "left" and "right" views that it combines each say that the column is a float for some reason. It's possibly because there's an ISNULL operation going on over the original integer value... I'm not sure.

Anyway, the two sub-views have a column that SQL has, for some unknown reason, decided to think of as a float. They get combined together in a third view that seems to understand that we're expecting an integer. At runtime, however, I think the result is being returned to Entity Framework as a float rather than an int, and this makes EF sad, so it throws this error.

Solution: I added a CAST(<stuff> AS INT) in each of the two sub-views, and the problem went away.

Mel
  • 2,337
  • 19
  • 25