Most of the tutorials on the internet seem to focus on how to remove these objects from DataContext
as opposed to DbContent
. However, if your database is built using code first approach you are unlikely to be using DataContext
/. In order to delete an entry from the database, firstly Remove the element and then save the changes. Check out the example below:
1 2 3 4 5 6 7 8 9 10 |
using(MyContext myContext = new MyContext) { var query = from u in myContext.Users where u.UserID == 23 select u if(query.Count() > 0) { myContext.Users.Remove(query.SingleOrDefault()); myContext.SaveChanges(); } } |
Seems to be pretty simple...