This error message is appearing when I run my program

-1

When I run my program, the following error occurs:

>Error  1   'AgendaMVC.Models.AgendaDBContext' does not contain a definition

for 'Agenda' and no extension method 'Agenda' accepting a first argument of type 'AgendaMVC.Models.AgendaDBContext' could be found (are you missing a using directive or an assembly reference?)

C:\curso\Agenda\Agenda\Controllers\AgendaController.cs  33  35  Agenda

What can it be?

********** class agendaDBContext *******************

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 
using AgendaMVC.Models;

namespace AgendaMVC.Models 
{ 
    public class AgendaDBContext : DbContext 
    {    
        public AgendaDBContext() :base("name=AgendaDBContext")
        {

        } 

        DbSet Agendas { get; set; } 
    } 
}       

Controller:

using AgendaMVC.Models;
using System;
using System.Web.Mvc;

namespace AgendaMVC.Controllers
{
    public class AgendaController : Controller
    {
       AgendaDBContext agendaContext = new AgendaDBContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Inserir() 
        {
            return View();
        }

        [HttpPost]
        public ActionResult Inserir(Agenda agenda) 
        {
            if (ModelState.IsValid) 
            {
                try
                {
                    agendaContext.Agenda.Add(agenda);//o problema esta aqui agendaContext //nao reconhece a agenda
                    agendaContext.SaveChanges();

                    return Json(new
                    {
                        Url = Url.Action("Index"),
                        Mensagem = "Contato Inserido com sucesso",
                        Titulo = "Sucesso"
                    });
                }
                catch (Exception) {
                    throw;
                }
            }

            return View(agenda);
        }    
    }
}
    
asked by anonymous 06.11.2014 / 17:44

1 answer

2

Based on what I was able to deduce from your "AgendaDBContext" class, the problem is that the Agenda variable is not public.

In C #, when the access modifier is omitted, private is inferred. That is, when you do:

DbSet Agendas { get; set; } 

It means that this variable is visible only within the ScheduleDBContext class.

To resolve the problem, include the public:

namespace AgendaMVC.Models 
{ 
    public class AgendaDBContext : DbContext 
    {    
        public AgendaDBContext() :base("name=AgendaDBContext")
        {
        } 
        public DbSet Agendas { get; set; } 
    } 
}
    
06.11.2014 / 18:27