heres my code:
User user = db.Users.Where(u => u.ID == userInSession.ID).FirstOrDefault();
UserItem UI = user.UserItems.Where(ui => ui.User == user && ui.Item == item&& ui.IsFavourite == true).FirstOrDefault();
if (UI == null)
{
return false;
}
else
{
user.UserItems.Remove(UI);
return true;
}
db.SaveChanges();
It’s finds the erntry and tries to remove it, but after removing it, it doesn’t delete the entire row, it just deletes the value of UserID in the table. What am I missing here? When I do .Add it works fine..
Use
db.UserItems.Removeto remove the actual object. Also you can create a single query to check if the UserItems exists for that specific userSessionId as below :