Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

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

(0) ShareShare
ReportReport
Posted on by 145

Hi,

I have generated Xrm for my CRM instance , when i use the classes in it which represents schema names of an entities , exception occurs . For example, i have entity called "Class" and it's schema name is "new_class", when i create a query like this :

using (var orgService = crmConnection.GetContext())
{
  return (from i in orgService.CreateQuery<new_instructorplacement>()
          where i.Id == id
          select i).FirstOrDefault();
                   
}

exception occurs : {"Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'new_instructorplacement'."}. Any one knows what might be the problem ?

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity'

    Hi,

    Can you share the definition of your class? As mentioned above, based on the error message, it looks like new_instructorplacement doen't inherit from Microsoft.Xrm.Sdk.Entity.

  • AllanDeCastro Profile Picture
    AllanDeCastro 156 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity'

    Hello,

    Just try to do it in late bound to see the result and post it here plz  :

    return (from i in orgService.CreateQuery("new_instructorplacement")

         where i.GetAttributeValue<Guid>("new_instructorplacementid")==id

          select i).FirstOrDefault();

    "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'new_instructorplacement" suggests that new_instructorplacement is not an considered as an entity

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 1,019 Super User 2025 Season 1 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity'

    Hi Khaled Rimawi,

    What are you trying to do with your code?

  • Khaled Rimawi Profile Picture
    Khaled Rimawi 145 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity'

    @Adrian , that's how i generated it

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 1,019 Super User 2025 Season 1 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity'

    Hi Khaled Rimawi,

    How did you generate the classes? I recommend creating early bound entity classes is with the code generation tool (CrmSvcUtil.exe). You can find information on this here.

  • Khaled Rimawi Profile Picture
    Khaled Rimawi 145 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity'

    Sorry , i have put a code for an entity called instructor placement instead of class, it's schema name is "new_instructorplacement"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans