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
Customertable but onlySchedDatecolumn returned, hence EF cannot done mapping other omitted columns includingCustID.Assuming
Customersis aDbSet<Customer>, try return all fields fromCustomerinstead:If you want just returning
SchedDatecolumn, materialize query results and useSelectafterwards: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