Why am I getting "is inaccessible due to its protection leve

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Protection
Click For Summary

Discussion Overview

The discussion revolves around an error encountered in Visual Studio related to the accessibility of the DbSet constructor in Entity Framework. Participants explore potential causes and solutions for the error message indicating that the DbSet constructor is inaccessible due to its protection level.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports an error stating that the DbSet constructor is inaccessible and questions the role of access modifiers in this issue.
  • Another participant suggests that the problem might be related to operating system-level access rights, prompting a check of Windows permissions in the folder.
  • A third participant confirms the user directory path as correct, indicating a focus on basic troubleshooting steps.
  • Another possibility raised is the need for a database sign-on that may have been overlooked.
  • A later reply proposes that the inaccessibility of the DbSet constructor is due to its protected status, suggesting that instances should be created from the context object instead.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the error, with no consensus reached on the underlying issue or the best approach to resolve it.

Contextual Notes

There is an assumption that the DbSet constructor's protection level is the primary concern, but this is not universally accepted among participants. The discussion also highlights potential external factors such as operating system permissions and database authentication that may influence the error.

SlurrerOfSpeech
Messages
141
Reaction score
11
Visual Studio is giving me the error

Error 1 'System.Data.Entity.DbSet<Survey.Models.Survey>.DbSet()' is inaccessible due to its protection level c:\users\jamin\documents\visual studio 2013\Projects\Survey\Survey\Models\SurveyDbModel.cs 83 40 Survey

on the line

Code:
DbSet<Survey> AllSurveys = new DbSet<Survey>();

of

Code:
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Survey.Models
{

    public class Survey
    {
        public int Id { get; set; }
        [StringLength(100)]
        public string Title { get; set; }
    }

    public class Parnter
    {
        public int Id { get; set;}
        [StringLength(50)]
        public string Name { get; set; }
    }

    public class Question
    {
        public int Id { get; set; }
        public int SurveyId { get; set; }
        [StringLength(300)]
        public string QText { get; set; }
    }

    public class Answer
    {
        public int Id { get; set; }
        public int QuestionId { get; set; }
        public int PartnetId { get; set; }
        public decimal Val { get; set; }
    }

    public class SurveyDbModel
    {
        // name of connection string for database that
        private static readonly string _ConnStrName = "LocalDb";

        private SqlConnection _Conn;

        public SurveyDbModel ( )
        {
            this._Conn = new SqlConnection(ConfigurationManager.ConnectionStrings[SurveyDbModel._ConnStrName].ConnectionString);
        }

        ~SurveyDbModel ( )
        {
            this._Conn.Dispose();
        }

        public void AddSurvey ( string survtitle )
        {
            SqlCommand cmd = new SqlCommand("AddSurvey", this._Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@title", survtitle);
            this._Conn.Open();
            cmd.ExecuteNonQuery();
            this._Conn.Close();
        }

        public void DeleteSurvey ( int survid )
        {
            SqlCommand cmd = new SqlCommand("DeleteSurvey", this._Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", survid);
            this._Conn.Open();
            cmd.ExecuteNonQuery();
            this._Conn.Close();
        }

        public DbSet<Survey> GetAllSurveys ( )
        {
            SqlCommand cmd = new SqlCommand("GetAllSurveys", this._Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            this._Conn.Open();
            DbSet<Survey> AllSurveys = new DbSet<Survey>();
            using ( SqlDataReader dataReader = cmd.ExecuteReader() )
            {
                while ( dataReader.Read() )
                {
                    Survey srv = new Survey
                    {
                        Id = int.Parse(dataReader[0].ToString()),
                        Title = dataReader[1].ToString()
                    };
                    AllSurveys.Add(srv);
                }
            }
            this._Conn.Close();
            return AllSurveys;
        }

        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;
        }

    }
}

Any idea why? Personally, I can't see how any access modifiers are getting in the way of me creating a DbSet<T> where T is a Survey.
 
Technology news on Phys.org
Looks like an access problem on the OS level. Check you're Windows rights in the folder.
 
SlurrerOfSpeech said:
c:\users\jamin
Just checking the obvious - that is your user directory, correct?
 
Another possibility is that you might need and have forgotten a DB sign-on.
 
SlurrerOfSpeech said:
Visual Studio is giving me the error

Error 1 'System.Data.Entity.DbSet<Survey.Models.Survey>.DbSet()' is inaccessible due to its protection level c:\users\jamin\documents\visual studio 2013\Projects\Survey\Survey\Models\SurveyDbModel.cs 83 40 Survey

on the line

Code:
DbSet<Survey> AllSurveys = new DbSet<Survey>();
...
I think it's because the constructor of DbSet<T> is protected and so its instance in this case should be created from the context object.
 

Similar threads

Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 28 ·
Replies
28
Views
4K
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K