CRM 2011 Error: Object of type 'Microsoft.Xrm.Sdk.Entity' cannot be converted to Type

While I was working with Querying Contact data for CRM 2011 through LINQ, I faced this error which says:

Object of type ‘Microsoft.Xrm.Sdk.Entity’ cannot be converted to Contact

My code goes like this:

OrgService = new OrganizationServiceProxy(OrgServiceUri, null, this.ccreds, this.dcreds);
using (var context = new CrmDataContext(OrgService))
{
    var query = from contacts in context.ContactSet
                select new Contact
                {
                    FirstName = contacts.FirstName,
                    Address1_Country = contacts.Address1_Country
                };

    foreach (var contact in query)
    {
        ContactsListBox.Items.Add(contact.FirstName);
    }
}

After little searching on the net for this error, I got the solution to prevent this error. You need to add following statement:

OrgService.EnableProxyTypes();

I got the reference from this. And it is required to enable support for Early bound entity types.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s