This error message appears when running my program

Asked

Viewed 88 times

-1

When running my program the following error arises:

>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 be?

**********scheduleDBContext class*******************

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);
        }    
    }
}
  • inform your context, error, doubt and your searches please.

  • this is the error that appears what can be?

  • Post the C code: course Agenda Agenda Controllers Agendacontroller.Cs

  • No, you don’t get bored. in the question you should put the context! , that is, explain what your program does, put the error shown, and the code snippet where you think you have the error. type like: http://answall.com/questions/39511/problema-com-conex%C3%A3o-ftp-no-android

  • as I do, because when I do exceeds the limit

  • not in the comment, edit your question (below the tags between share and flag)

  • using Agendamvc.Models; using System; using System.Web; using System.Web.Mvc; namespace Agendamvc.Controllers { public class Agendacontroller : Controller { Agendadbcontext scheduleContext = new Agendadbcontext(); public Actionresult Insert() { Return View(); } [Httppost] public Actionresult Insert(Schedule) { if(Modelstate.Isvalid) { Try { scheduleContext.Agenda.Add(scheduler); scheduleContext.Savechanges(); Return Json(new { Url = Url.Action("Index"), Message = "Contact Successfully Inserted", Title = "Success" }); } } ; (Exception) { throw; } } Return View(agenda); } } }

  • 1

    @Eversonsouzadearaujo You can change the question and include the code of the Agendadbcontext class?

Show 3 more comments

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 Agendadbcontext class.

To solve the problem, include the public modifier:

namespace AgendaMVC.Models 
{ 
    public class AgendaDBContext : DbContext 
    {    
        public AgendaDBContext() :base("name=AgendaDBContext")
        {
        } 
        public DbSet Agendas { get; set; } 
    } 
}
  • show worked, I did not pay attention to this detail

  • @Eversonsouzadearaujo Legal. Since the solution worked, the ideal is that you mark it as the right answer.

  • The first time I use this forum I still don’t know, how I do?

  • @Eversonsouzadearaujo Follow the link by explaining: http://answall.com/tour. In the left corner of my answer there are up and down arrows, among them is a number indicating whether the answer is useful or not, if you think the answer is useful you mark up and of course do the opposite when it is not. To mark the answer as accepted you click on the gray check just below this control. The same will go green indicating the chosen answer as correct.

  • 1
  • Thank you @Andrefigueiredo

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.