nat
nat

Reputation: 2197

The data reader is incompatible with the specified class

so there are a few other similar type posts about, but none matching the simple task I am attempting. just running a stored proc through EF6 code first in the following manner

var results = context.DataBase.SqlQuery<MyClass>("exec spGetStuff @param1, @param2", new SqlParameter[] {new SqlParameter("param1",value), new SqlParameter("param2", value2)});

I have used this method on many occasions with no issue. the class I am mapping the results to is pretty nasty with many properties, but all that need it are marked with the [Column("dbfieldname")] attribute.

all the stored proc is doing is returning some results by using a

SELECT * FROM(
SELECT 
    ROW_NUMBER() OVER ( PARTITION BY X,Y,Z ORDER BY A) [RowNumber]
,*
FROM
MyTableNAme
WHERE
...) S
WHERE s.RowNumber = 1

not inserting, updating or anything fantastical like that.

The data reader is incompatible with the specified 'MyClass'. A member of the type, 'PropertyNameName', does not have a corresponding column in the data reader with the same name.

if I do change the class properties to the db column names it seems to work fine:I can change the first few properties and it will then fail on other ones in the class... however I do not really want to do that if I can possibly avoid it as most of the columns in the DB are named very badly indeed, so I guess my question is why is it ignoring the [Column()] attributes that have never failed me before. or is the issue the row_number, which I have tried adding to the class an/or removing from the query resultsset.

Upvotes: 0

Views: 2001

Answers (1)

adillon
adillon

Reputation: 21

We're seeing this issue too since upgrading and refactoring for EF6 from 5. Stored procedure returns and we have a Complex Type defined in our edmx. It seems that everything matches up but we get the same type of error when calling like this

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetColumnValueSet_Result>("GetColumnValueSet", param1, param2, param3);

So after trying a few things this is what I found. I opened my edmx and went to the Model Browser within Visual Studio. Under Function Imports I found my sproc. Right clicked and chose Function Import Mapping. It turned out that even though the complex type was correct and it appeared everything should have matched up the Function Import Mapping was thinking the sproc was returning a column called CodeValue instead of Value (which is what it was actually importing).

So for some reason when the sproc columns were gotten it go a wrong name for the mapping.

Upvotes: 1

Related Questions