Unable to cast object to 'System.Data.Objects.ObjectContext'

Last post 07-15-2009 4:48 PM by german00. 4 replies.

Sort Posts:

  • Unable to cast object to 'System.Data.Objects.ObjectContext'

    04-13-2009, 10:35 PM
    • Member
      point Member
    • tkr
    • Member since 01-22-2009, 9:17 PM
    • Posts 6

    Hello,

    I am trying to do my second Dynamic Data project.  I am just working on setting it up and I created the project, the LinqToSQL class, and updated the Global.asax file with the correct data context.  When I run my project, I get the page that lists the three tables that were added to my LinqToSQL class, however when I click one of the tables, I get the following error:

    Unable to cast object of type 'AFP_DPS_DD.AFP_DPSDataContext' to type 'System.Data.Objects.ObjectContext'.

    The first project I did with a different datasource did not have this issue.  Any idea what I am doing wrong?

    Thanks

  • Re: Unable to cast object to 'System.Data.Objects.ObjectContext'

    04-14-2009, 6:45 AM
    • Contributor
      2,060 point Contributor
    • andreadottor
    • Member since 01-25-2007, 10:21 AM
    • Italy
    • Posts 312
    • TrustedFriends-MVPs

    Are you sure that use the correct project template? 

    Linq2sql and Entity Framework have 2 different template project, because the templates and pages are a little different based on which model type is used.

    With Linq2Sql models you need to use the Dynamic Data project.
    With EntityFramework models you need to use the Dynamic Data Entities.

    If this answers your question, please select 'mark as answer' thanks!



    Andrea Dottor
    Microsoft MVP - ASP/ASP.NET
  • Re: Unable to cast object to 'System.Data.Objects.ObjectContext'

    04-14-2009, 8:29 AM
    • Star
      12,250 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,558
    • TrustedFriends-MVPs

    Andrea is right you must be using the Entity Framework model look in the DyanmicData\PageTemplate\List.aspx page and see what data source it is using if it is the EntityDataSource you have setup the wrong project. Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Unable to cast object to 'System.Data.Objects.ObjectContext'

    04-14-2009, 10:06 AM
    • Member
      point Member
    • tkr
    • Member since 01-22-2009, 9:17 PM
    • Posts 6

    Right on.  That is exactly what I did.  I changed the project type and I am good to go.

  • Re: Unable to cast object to 'System.Data.Objects.ObjectContext'

    07-15-2009, 4:48 PM
    • Member
      2 point Member
    • german00
    • Member since 06-25-2008, 11:12 AM
    • Posts 1

    Work for my, thanks

Page 1 of 1 (5 items)