EF 4.0 Dynamic proxies POCO Object Does not match target type

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?

+3
source share
3 answers

I don't know if the problem has been resolved yet, but I had the same problem with the Script class (POCO).

context.CreateObject<Scenario> (POCO) i.s.o. a.... = ().

+1

, GridView . ,

  • TemplateFields, , GridView.

  • Linq ,

      So instead of using something like ti 
    
        gvGroups.DataSource = ProductHelper.Get()
        gvGroups.DataBind();
    
    
    
        var query = from p in ProductHelper.Get()
        select new {p.ProductId, p.ProductName, p.ProductDesc, p.ProductLink};
        gvGroups.DataSource = query;
        gvGroups.DataBind();
    
+2

Value Injecter . , :

var dynamicProxyMember = _repository.FindOne<Member>(m=>m.Id = 1);
var member = new Member().InjectFrom(dynamicProxyMember) as Member;

:)

+1

Source: https://habr.com/ru/post/1751229/


All Articles