I've been looking around but can't seem to find a solution. I'm not sure why, but db.SaveChages() is not saving the changes to database.
What I am trying to do is to increment the views counter by 1.
PictureViewCounter first calls the GetSinglePicture which returns the picture record.
PictureHelper.PictureViewCounter(PictureHelper.GetSinglePicture(userID,pic), (List<int>)Session["ViewedPictures"], pic);
PictureViewCounter first calls the GetSinglePicture which returns the picture properties.
public static Picture GetSinglePicture(int userID, int picture)
{
DBContext db = new DBContext();
return (from p in db.Pictures
where userID == p.UserID && picture == p.Url
select p).First();
}
PictureViewCounter then iterates through the array of pictureArray and sees if there is a match, if no match, increment the picture.Views.
public static void PictureViewCounter(Picture picture, List<int> pictureArray, int pictureUrl)
{
PetscoveryDBContext db = new PetscoveryDBContext();
if (pictureArray == null)
{
//Automatically +1 picture views
picture.Views++;
db.SaveChanges(); //does not save
}
else
{
int result = pictureArray.Find(pic => pic == pictureUrl);
//if result == 0 increase picture views by 1
picture.Views++;
db.SaveChanges(); //does not save
}
}
It works when debugging, the Views do get incremented, just not saved. It's probably something VERY simple.
Thanks