erinql
erinql

Reputation: 141

Data reader is incompatible... member does not have corresponding column in data reader

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

Answers (6)

Leon
Leon

Reputation: 1

Easy Way:

  1. Once SP is complete update EF Model so it is available.
  2. Add PRINT statement to SP, re-compile and run.
  3. Comment out Dynamic SQL and put in PRINT statement, recompile ensure it runs.
  4. Now do Function Import in EF, columns will show.
  5. Change SP back to Dynamic code and all good :)

Upvotes: 0

Bob Sheehan
Bob Sheehan

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

santos
santos

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

JamieA
JamieA

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

Calvin Fisher
Calvin Fisher

Reputation: 4701

Rather than updating the view, you can also update the Function Import:

  • Go to the Model Browser window
  • Expand the EntityContainer
  • Open the Function Import in the Mapping Details window
  • If the entity name (left column) does not match the expected field name (right column), you can change the right-hand column to match what the returned field is actually called.

Upvotes: 12

erinql
erinql

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

Related Questions