Reputation: 141
Using VS 2008, SQL Server 2008 and WPF 3.5, I've made some changes to my schema and updated the model. It compiles and runs fine, until the client app calls for a specific entity and I get the following (actual names replaced):
The data reader is incompatible with the specified '<Model>.<ViewBasedEntity>'. A member of the type, '<Property>', does not have a corresponding column in the data reader with the same name.
I've searched through the services app for related entity and property names, tried renaming properties in the Table Map, Seems to be a number of others out there reporting the same error, but can't seem to find a timely answer....
...Does anyone know how to track this down, and if so, is there a fix or methodology to follow to avoid in the future?
Upvotes: 10
Views: 29814
Reputation: 1
Easy Way:
Upvotes: 0
Reputation: 160
Ive seen this happen on data objects that vary the result set (for example a sproc with an if statement) The error message is Entity Frame work/ Data reader telling you that it expected column x and it wasn't returned.
To work around this you can either a) Make sure all paths of your sproc return the same column names b) Use Database.ExecuteSqlCommand
Upvotes: 2
Reputation: 484
I came up against the same issue when using Sprocs recently. I had some conditonal SQL in the sproc along the lines of a couple of 'if' statements
IF @param1 = 'knownValue'
BEGIN
SELECT * FROM EntityType WHERE ID = 'somevalue'
END
ELSE IF @param1 = 'knownValue2'
BEGIN
SELECT * FROM EntityType WHERE ID = 'somevalue'
END
here's my EF code:
return context.Database.SqlQuery<EntityType>(
"[NAV].[GeEntityType] @Date, @ID",
new SqlParameter("Date", paramDate),
new SqlParameter("ID", paramId)).ToList();
I had not catered for scenarios where neither 'if' statements returned true, so the sproc wasn't even returning an empty resultset and caused EF to throw this error. Without returning even an empty reader, EF had nothing to match when mapping columns to properties.
Hope it helps.
Upvotes: 1
Reputation: 2013
I had a similar issue which produced the same error message - the problem was that a column name returned by the proc included a space.
When creating the complex type, [my column]
was created as my_column
.
Then when executing the proc with ExecuteStoreQuery
, my_column
did not exist in the data reader, as the proc still returned [my column]
.
Solution: remove the space from proc column name and recreate your complex type for the imported function.
Upvotes: 5
Reputation: 4701
Rather than updating the view, you can also update the Function Import:
Upvotes: 12
Reputation: 141
Ok - here's the skinny:
That particular view was setup as a Return Type for a Stored Procedure that had to be setup as a Function Import in the Model's Entity Container.
I had updated that view to fit new reporting requirements, not realizing it's significance to the Function Import. The additional fields are not part of the data set recognized for this purpose, so it couldn't find a match for any of them.
So I duplicated the view and suffixed it with 'Report', then reverted the original back to it's expected set of return fields.
Voila!
Upvotes: 4