locked
Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type RRS feed

  • Question

  • when I using below code :

     using (ServiceContext svcContext = new ServiceContext(service))
                {
                    var creditNote = svcContext.new_credit_noteSet.Where(c => c.new_credit_noteId == cnid).FirstOrDefault();

    ...

    }

    got this error:

    System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'TCT.CRM.Model.new_credit_note'.
       at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
       at Microsoft.Xrm.Sdk.Linq.PagedItemCollection`1.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
       at HP.Crm2013.Plugins.ServiceManage.sm_credit_note_update_post.CalculateTotalAmount(IOrganizationService service, Guid cnid)
       at HP.Crm2013.Plugins.ServiceManage.sm_credit_note_update_post.Execute(IServiceProvider serviceProvider)

    any help?

    Awen

    Wednesday, December 31, 2014 9:21 AM

All replies

  • Hi Awen,

    This kind of error usualy occur if we dont have enable proxytype for the CRM service.

    Add the below line in your code after creating the service proxy and check :

    _serviceProxy.EnableProxyTypes();

    Thanks,

    Seban

    Wednesday, December 31, 2014 9:58 AM