4

Can someone help me understand this error? I'm getting it on the line new DbSet<Survey>(); in the block code

    public DbSet<Survey> GetAllSurveys ( )
    {
        DbSet<Survey> AllSurveys = new DbSet<Survey>();
        using ( SqlCommand cmd = new SqlCommand("GetAllSurveys", this._Conn) )
        {
           cmd.CommandType = CommandType.StoredProcedure;
           this._Conn.Open();

           using ( SqlDataReader dataReader = cmd.ExecuteReader() )
           {
                while ( dataReader.Read() )
                {
                    Survey srv = new Survey { Id = dataReader.IsDBNull(0) ? default(int) : dataReader.GetInt32(0),
                                              Title = dataReader.IsDBNull(1) ? String.Empty: dataReader.GetString(1) };
                    AllSurveys.Add(srv);
                }
           }
           this._Conn.Close();
        }
        return AllSurveys;
    }

What's strange is that I'm not getting it when I create a DbSet<T> right below:

    public DbSet<Question> GetQuestionsBySurveyId ( int survid )
    {
        SqlCommand cmd = new SqlCommand("GetQuestionsBySurveyId", this._Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", survid);
        this._Conn.Open();
        DbSet<Question> TheseQuestions = (DbSet<Question>)cmd.ExecuteScalar();
        this._Conn.Close();
        return TheseQuestions;
    }

has no errors associated with it. What's the difference between these two?

3
  • In what line exactly you're getting this error? Is it a compiler error? Commented Jan 29, 2016 at 5:50
  • 3
    DbSet is an abstract class, of course you can't create a new one directly, but can cast it. msdn.microsoft.com/en-us/library/… Commented Jan 29, 2016 at 5:53
  • Maybe you are looking for DataTable? stackoverflow.com/questions/6073382/… Commented Jan 29, 2016 at 5:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.