Is it possible to help me with fixing error inside of create action methods? Everything else inside of website is fine. But when i try to add Create view the Create action method is showing current error. I think that it has something to do with references
those of which have already tried to add, but still getting error. Markus.
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'db' does not exist in the current context AndrewProject C:\Users\Andrew\Documents\Visual Studio 2015\Projects\AndrewProject\AndrewProject\Controllers\PersonController.cs 48 Active
namespace AndrewProject.Controllers
{
public class PersonController : Controller
{
// GET: Person
private PersonsEntities _db = new PersonsEntities();
public DbSet<Persons> Persons { get; set; }
public ActionResult Index()
{
return View(_db.Persons.ToList());
}
public ActionResult Create()
{
return View();
}
// POST: Team/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Age, FirstName, LastName, City")] Persons person)
{
if (ModelState.IsValid)
{ db.Persons.Add(person); //this is the errors, the
db connections are having errors?(bold) db.SaveChanges();
return RedirectToAction("Index");
}
Member
251 Points
423 Posts
Database predicate not working.
Oct 19, 2018 04:42 PM|Markus33|LINK
Is it possible to help me with fixing error inside of create action methods? Everything else inside of website is fine. But when i try to add Create view the Create action method is showing current error. I think that it has something to do with references those of which have already tried to add, but still getting error. Markus.
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'db' does not exist in the current context AndrewProject C:\Users\Andrew\Documents\Visual Studio 2015\Projects\AndrewProject\AndrewProject\Controllers\PersonController.cs 48 Active
namespace AndrewProject.Controllers
{
public class PersonController : Controller
{
// GET: Person
private PersonsEntities _db = new PersonsEntities();
public DbSet<Persons> Persons { get; set; }
public ActionResult Index()
{
return View(_db.Persons.ToList());
}
public ActionResult Create()
{
return View();
}
// POST: Team/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Age, FirstName, LastName, City")] Persons person)
{
if (ModelState.IsValid)
{
db.Persons.Add(person); //this is the errors, the db connections are having errors?(bold)
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
}
}
All-Star
52141 Points
23248 Posts
Re: Database predicate not working.
Oct 19, 2018 05:52 PM|mgebhard|LINK
You declared the context as _db not db.
As recommended in your other threads with the same issues, please go through the Getting Started with EF Tutorials in this site.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application