Thari Thari - 1 month ago 10
C# Question

How to delete a database record using c# entity framework?

I'm using asp.net c# with entity framework and i have tried to delete a record from database. So for that i have coded the controller part as below.

[HttpPost]
public ActionResult DeleteAccount(DeleteAccountViewModel deleteaccountviewmodel)
{
TheFoodyContext db = new TheFoodyContext();
string UserEmail = Session["UserEmail"].ToString();
User user_to_update = db.Users.SingleOrDefault(s => s.email == UserEmail);

if (user_to_update != null)
{
if ((deleteaccountviewmodel.Password == user_to_update.password))
{
db.Users.Remove(user_to_update);
db.SaveChanges();
return RedirectToAction("~/View/Home");

}
}
//return RedirectToAction("~/View/Home");
}


But in here it will give me an error because it doesn't identify 'Stores'. I don't know why is that. Can anyone help me?

Answer

your code seems to be correct. db.SaveChanges(); will return value you can check whether it is removed or not. but you are mentioned 'Stores' not identified means if you are trying to delete record from Stores table use db.Stores.Remove() if not explain clearly your problem

Comments