Thari Thari - 1 year ago 122
C# Question

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

I'm using 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.

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

if (user_to_update != null)
if ((deleteaccountviewmodel.Password == user_to_update.password))
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download