A member of the type, 'PropertyName', does not have a corresponding column in the data reader with the same name

c# ef-code-first entity-framework-6

Question

I'm using Entity Framework 6, code first, and using the ObjectContext.Translate() method to execute a stored procedure to fill a list of objects.

Do I need to return every property of the class in the stored procedure? Or can I return some properties?

The MSDN article Directly Executing Store Commands suggests I should be able to return some properties:

If the name of the type's property does not match a field of the DbDataReader, the Entity Framework materializes the default value of the property if it is defined in the conceptutal model.

But when I leave a property out of the stored procedure I get an error. For example, assume MyClass with 3 properties: Id, Name, Description. If my stored procedure returns the following:

SELECT Id, Name, Description from dbo.MyTable;

all is well. But if I return just two properties, leaving one out:

SELECT Id, Name from dbo.MyTable;

and then call (where "db" is an instance of the DbContext and "rdr" is an instance of a DbDataReader):

((IObjectContextAdapter)db).ObjectContext.Translate<MyClass>(rdr).ToList();

I get the error message "A member of the type, 'Description', does not have a corresponding column in the data reader with the same name."

Am I misunderstanding the MSDN article? Can I return some properties from a stored procedure when calling ObjectContext.Translate()? Or must I always return all properties?

Or does the phrase "materializes the default value of the property if it is defined in the conceptutal (sic) model" mean I need to add some annotations to my model class?

1
0
5/26/2015 8:31:55 PM

Accepted Answer

Here is what appears to be the problem: my project lacks a "conceptual model." That is to say, I'm using plain old clr objects (POCO) and did not generate *.edmx files using the entity data model wizard.

As MSDN states:

If the name of the type's property does not match a field of the DbDataReader, the Entity Framework materializes the default value of the property if it is defined in the conceptutal model.

Since I do not have a conceptual model, I assume that is the reason Entity Framework does not "materialize the default value" for me and instead throws an exception.

I assume that if I generated the *.edmx files (which for various reasons I cannot do in this project) and used those entities instead of my POCO entities the error message would disappear.

0
5/27/2015 1:27:58 PM

Popular Answer

If seems that ObjectContext.Translate requires that all properties are present.

The supplied DbDataReader must contain data that maps to the requested entity type.

https://msdn.microsoft.com/en-us/library/dd466384%28v=vs.110%29.aspx



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow