Erro DbContex "ListaCursosContext" é inacessível devido ao seu nível de proteção

Humberto Gonçalves 1 Reputation point
2021-06-09T14:43:04.153+00:00

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace Model.Cursos
{
class ListaCursosContext : DbContext
{

    public ListaCursosContext()
        : base("Ctx_BolsasAgora") { }

    public DbSet<CursosModel> cursosModels { get; set; }

    public DbSet<Instituicoes.CampusModel> CampusModels { get; set; }

    public DbSet<Instituicoes.InstituicoesModel> InstituicoesModels { get; set; }

    public DbSet<Usuarios.UsuariosModel> UsuariosModels { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

}

using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Model.Cursos;

namespace BolsasAgora.Controllers
{
public class ListaCursosController : Controller
{
private ListaCursosContext db = new ListaCursosContext();

    // GET: ListaCursos
    public ActionResult Index()
    {
        var cursosModels = db.cursosModels.Include(c => c.CampCurso).Include(c => c.InstCurso).Include(c => c.UserCurso);
        return View(cursosModels.ToList());
    }

    // GET: ListaCursos/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        CursosModel cursosModel = db.cursosModels.Find(id);
        if (cursosModel == null)
        {
            return HttpNotFound();
        }
        return View(cursosModel);
    }

    // GET: ListaCursos/Create
    public ActionResult Create()
    {
        ViewBag.IdCampus = new SelectList(db.CampusModels, "IdCampus", "NomeCampus");
        ViewBag.IdInstituicao = new SelectList(db.InstituicoesModels, "IdInstituicao", "NomeInstituicao");
        ViewBag.IdUsuario = new SelectList(db.UsuariosModels, "IdUsuario", "Email");
        return View();
    }

    // POST: ListaCursos/Create
    // Para proteger-se contra ataques de excesso de postagem, ative as propriedades específicas às quais deseja se associar. 
    // Para obter mais detalhes, confira https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "IdCurso,IdInstituicao,IdUsuario,IdCampus,NomeCurso,DescricaoCurso,Categoria,Modalidade,Turno,Duracao,QtdeParcelas,QtdeBolsas,ValorMensalidade,PercentualDesconto,ValorDesconto,DataCadastro,DataModificacao")] CursosModel cursosModel)
    {
        if (ModelState.IsValid)
        {
            db.cursosModels.Add(cursosModel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.IdCampus = new SelectList(db.CampusModels, "IdCampus", "NomeCampus", cursosModel.IdCampus);
        ViewBag.IdInstituicao = new SelectList(db.InstituicoesModels, "IdInstituicao", "NomeInstituicao", cursosModel.IdInstituicao);
        ViewBag.IdUsuario = new SelectList(db.UsuariosModels, "IdUsuario", "Email", cursosModel.IdUsuario);
        return View(cursosModel);
    }

    // GET: ListaCursos/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        CursosModel cursosModel = db.cursosModels.Find(id);
        if (cursosModel == null)
        {
            return HttpNotFound();
        }
        ViewBag.IdCampus = new SelectList(db.CampusModels, "IdCampus", "NomeCampus", cursosModel.IdCampus);
        ViewBag.IdInstituicao = new SelectList(db.InstituicoesModels, "IdInstituicao", "NomeInstituicao", cursosModel.IdInstituicao);
        ViewBag.IdUsuario = new SelectList(db.UsuariosModels, "IdUsuario", "Email", cursosModel.IdUsuario);
        return View(cursosModel);
    }

    // POST: ListaCursos/Edit/5
    // Para proteger-se contra ataques de excesso de postagem, ative as propriedades específicas às quais deseja se associar. 
    // Para obter mais detalhes, confira https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "IdCurso,IdInstituicao,IdUsuario,IdCampus,NomeCurso,DescricaoCurso,Categoria,Modalidade,Turno,Duracao,QtdeParcelas,QtdeBolsas,ValorMensalidade,PercentualDesconto,ValorDesconto,DataCadastro,DataModificacao")] CursosModel cursosModel)
    {
        if (ModelState.IsValid)
        {
            db.Entry(cursosModel).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.IdCampus = new SelectList(db.CampusModels, "IdCampus", "NomeCampus", cursosModel.IdCampus);
        ViewBag.IdInstituicao = new SelectList(db.InstituicoesModels, "IdInstituicao", "NomeInstituicao", cursosModel.IdInstituicao);
        ViewBag.IdUsuario = new SelectList(db.UsuariosModels, "IdUsuario", "Email", cursosModel.IdUsuario);
        return View(cursosModel);
    }

    // GET: ListaCursos/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        CursosModel cursosModel = db.cursosModels.Find(id);
        if (cursosModel == null)
        {
            return HttpNotFound();
        }
        return View(cursosModel);
    }

    // POST: ListaCursos/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        CursosModel cursosModel = db.cursosModels.Find(id);
        db.cursosModels.Remove(cursosModel);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

}

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,798 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-06-10T03:07:05.177+00:00

    Hi HumbertoGonalves-6682,
    Please change the protection level from private to public.
    Change the following code:

    private ListaCursosContext db = new ListaCursosContext();  
    

    to

    public ListaCursosContext db = new ListaCursosContext();  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.