none
Unable to cast object type 'System.Data.Entity.Infrastructure.DbQuery'1... error RRS feed

  • Question

  • I got this message box with this error in it while doing a Quick Start Tutorial on EF.  This message box appears to be thrown due to an exception in the CourseViewer_Load event handler.  I suspect the issue is EF related, but I don't know where to start since I'm new to it and this doesn't provide any detail on what caused this.  

    This is the link for the tutorial: http://msdn.microsoft.com/en-us/library/vstudio/bb399182(v=vs.100).aspx

    Does anyone know what would cause this or how it can be fixed?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace CourseManager
    {
        public partial class CourseViewer : Form
        {
            // Create an ObjectContext instance based on SchoolEntity
            private SchoolEntities schoolContext;
    
            public CourseViewer()
            {
                InitializeComponent();
            }
    
            private void closeForm_Click(object sender, EventArgs e)
            {
                // Dispose the object context.
                schoolContext.Dispose();
    
                // close the form
                this.Close();
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void CourseViewer_Load(object sender, EventArgs e)
            {
                // Initialize the ObjectContext
                schoolContext = new SchoolEntities();
    
                // Define a query that returns all Department objects and course objects, ordered by name.
                var departmentQuery = from d in schoolContext.Departments.Include("Courses")
                                      orderby d.Name 
                                      select d;
    
                try
                {
                    // Bind the ComboBox control to the query, which is executed during data binding.
                    // To prevent the query from being executed multiple times during binding, 
                    // it is recommended to bind controls to the result of the Execute method.
    
                    this.departmentList.DisplayMember = "Name";
                    this.departmentList.DataSource = ((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    // Get object for the selected department.
                    Department department = (Department)this.departmentList.SelectedItem;
    
                    // Bind the grid view to the collection of Course objects
                    // that are related to the selected Department object.
                    courseGridView.DataSource = department.Courses;
    
                    // Hide the columns that are bound to the navigation properties on Course.
                    courseGridView.Columns["Department"].Visible = false;
                    courseGridView.Columns["StudentGrades"].Visible = false;
                    courseGridView.Columns["OnlineCourse"].Visible = false;
                    courseGridView.Columns["OnsiteCourse"].Visible = false;
                    courseGridView.Columns["People"].Visible = false;
                    courseGridView.Columns["DepartmentId"].Visible = false;
    
                    courseGridView.AllowUserToDeleteRows = false;
                    courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
            }
    
            private void saveChanges_Click(object sender, EventArgs e)
            {
                try
                {
                    // Save object changes to the database, display a message and refresh the form.
                    schoolContext.SaveChanges();
                    MessageBox.Show("Changes saved to the database.");
                    this.Refresh();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
    

    Wednesday, July 23, 2014 5:29 PM

Answers

All replies

  • The error you are getting about getting DbQuery when ObjectQuery is a good sign you are using a more recent version of EF than the tutorial intended. To be honest I would find a more recent course to study, rather than try to fix it. The Data Developer Center is a good place to start.

    Thursday, July 24, 2014 12:06 AM
  • Hello,

    It seems that you generate the context class which is inherited from DbContext while the walkthrough uses ObjectContext, for their difference, you could refer to this article:

    http://social.msdn.microsoft.com/Forums/en-US/01aac967-5b31-45ee-ac72-9095d9f2494c/difference-between-dbcontext-versus-objectcontext-dbset-objectset-etc?forum=adonetefx

    Reverting Back to ObjectContext Code Generation:

    http://msdn.microsoft.com/en-us/data/jj556581.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 24, 2014 8:39 AM
    Moderator
  • I am also looking for a solution to this issue. Fred Bao's suggestions didn't lead to a solution.
    Monday, November 3, 2014 9:18 PM
  • Hello,

    You can change the code as mentioned below,

     private void CourseViewer_Load(object sender, EventArgs e)
            {
                //Initialize the ObjectContext
                schoolContext = new SchoolEntities();

                // Define a query that returns all Department  
                // objects and course objects, ordered by name.
                var departmentQuery = from d in schoolContext.Departments.Include("Courses")
                                      orderby d.Name
                                      select d;
                try
                {
                    // Bind the ComboBox control to the query, 
                    // which is executed during data binding.
                    // To prevent the query from being executed multiple times during binding, 
                    // it is recommended to bind controls to the result of the Execute method. 
                    this.departmentList.DisplayMember = "Name";
                    //this.departmentList.DataSource = ((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);
                    this.departmentList.DataSource = departmentQuery.ToList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    //Get the object for the selected department.
                    Department department = (Department)this.departmentList.SelectedItem;

                    //Bind the grid view to the collection of Course objects
                    // that are related to the selected Department object.
                    courseGridView.DataSource = department.Courses.ToList();

                    // Hide the columns that are bound to the navigation properties on Course.
                    courseGridView.Columns["Department"].Visible = false;
                    courseGridView.Columns["StudentGrades"].Visible = false;
                    courseGridView.Columns["OnlineCourse"].Visible = false;
                    courseGridView.Columns["OnsiteCourse"].Visible = false;
                    courseGridView.Columns["People"].Visible = false;
                    courseGridView.Columns["DepartmentId"].Visible = false;

                    courseGridView.AllowUserToDeleteRows = false;
                    courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    Tuesday, November 3, 2015 6:40 AM