bmanmike39
asked on
How do I write the code to delete a table row in my db from my MVC controller? (ASP.NET, MVC)
I'm using the Entity framework in my MVC project. I would like to delete a record when the user navigates to a specific controller. What is wrong with my code below? And how can I accomplish this?
The table name is: fooItem
The table name is: fooItem
MYENTITIES oe = new MYENTITIES();
fooItem j = oe.fooItems.ToList().Where(d => d.fooItemId == Id).First();
oe.fooItems.Remove(j);
oe.SaveChanges();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SQL Server topic removed.
ASKER
I'm trying to delete the row from my records, not check if it exist.
Yes, debug the code see whether it works or not.
ASKER
I'm a little confused. Doesn't this code do the same thing.
NOTE: It's very Important that It only deletes the sing record.
NOTE: It's very Important that It only deletes the sing record.
MYENTITIES oe = new MYENTITIES();
var f = oe.fooItems.Where(s => s.fooItemId = Id).FirstOrDefault();
oe.fooItems.Remove(f);
oe.SaveChanges();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! This was just what I was looking for.