I did raw SQL query below to select only certain fields from a table.
{
List<CustEmpVM> CustomerVMlist = new List<CustEmpVM>();
var cid = db.Customers.SqlQuery("select SchedDate from Customer where CustID = '@id'").ToList<Customer>();
}
But i keep getting the error of:
System.Data.Entity.Core.EntityCommandExecutionException
occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The data reader is incompatible with the specified
ALFHomeMovers.Customer
. A member of the type,CustID
, does not have a corresponding column in the data reader with the same name.
The exception message is pretty straightforward: the query expected to return full entity of
Customer
table but onlySchedDate
column returned, hence EF cannot done mapping other omitted columns includingCustID
.Assuming
Customers
is aDbSet<Customer>
, try return all fields fromCustomer
instead:If you want just returning
SchedDate
column, materialize query results and useSelect
afterwards:NB: I think you can construct LINQ based from the SELECT query above:
Similar issue:
The data reader is incompatible with the specified Entity Framework