I am using EF 4.0 and POCO. I stumbled upon this error by inserting records into the database.
The accessor property 'QualityReasonID' for the object 'BI.Entities.QualityReason' throws the following exception: 'The object does not match the type of target.'
Errors occur in the Databind in the GridView after saving a new record to the database. I have determined what is happening, but I'm not sure WHY this is happening or if I am using EF / POCO incorrectly. Any insight would be appreciated.
An exception occurs because the types of objects in IEnumerable do not match. The original entries in the table are of type System.Data.Entity.DynamicProxies.QualityReason_E483AD567288B459706092F1825F53B1F93C65C5329F8095DD1D848B5D039F04} While the new one is BI.Entities.QuailtyReason.
This is how I insert a new object.
public void createQualityReason(QualityReason qReasons)
{
dbcontext.QualityReasons.AddObject(qReasons);
dbcontext.SaveChanges();
}
I resolved the error by changing the fetch code:
public IEnumerable<QualityReason> fetchQualityReasons()
{
IEnumerable<QualityReason> queryReasons = dbcontext.QualityReasons.AsEnumerable();
return queryReasons;
}
to
public IEnumerable<QualityReason> fetchQualityReasons()
{
IEnumerable<QualityReason> queryReasons = from data in dbcontext.QualityReasons.AsEnumerable()
select new QualityReason
{
QualityReasonID = data.QualityReasonID,
QualityReasonName = data.QualityReasonName
};
return queryReasons;
}
Therefore, to get around the error, I have to choose the POCO class each time. It seems to me that I'm something wrong. Any thoughts?
source
share