views:

58

answers:

1

I am using EF 4.0 and POCO's. I stumbled across this error while inserting to records into the data base.

Property accessor 'QualityReasonID' on object 'BI.Entities.QualityReason' threw the following exception:'Object does not match target type.'

There errors occur on the Databind to a GridView after saving a new record to the database. I identified what is happening but I am not sure WHY it is occurring or If I am using EF/POCO's incorrectly. Any insight would be appreciated.

The exception is occuring because the object types in the IEnumerable are not the same. The orginal entrys in the table are of type System.Data.Entity.DynamicProxies.QualityReason_E483AD567288B459706092F1825F53B1F93C65C5329F8095DD1D848B5D039F04} While the new one is BI.Entities.QuailtyReason.

Here is how I insert the new object.

   public void createQualityReason(QualityReason qReasons)
    {
        dbcontext.QualityReasons.AddObject(qReasons);
        dbcontext.SaveChanges();   
    }

I resolved the error by changing the fetch code from:

 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;
    }

So to get around the error I have to select into the POCO class explicitly each time. This feels like I am going something wrong. Any thoughts?

A: 

Hi,

I don't know if the problem has been solved yet, but I've had the same problem with my (POCO) "Scenario" class.

The problem disappeared when using a context.CreateObject to create the (POCO) object i.s.o. a .... = new Scenario().

Hope it helps

Marcel Havekes (Ordina.nl)

Marcel Havekes