0

I have entity framework project and I want to reset the value when the day is changed, or tomorrow the value will reset into 0. I have two tables name Goal and Calculation. Goal table has goalId, calorieToday, and calorieGoal and Calculation table has goalId, date, and amount. I want to reset calorieToday and amount when the day is changed and it works on background. So when user open again the application for tomorrow, calorieToday and amount values will became 0.

This is inside of GoalDataContext:

public DbSet<Goal> Goals { get; set; }
public DbSet<Calculation> Calculations { get; set; }

Edit: What I want is erase all value of calorieToday when day is changes. Everyday, the calorieToday value is became 0 after changes the day. The goalId from Goal is Primary key and also the foreign key for Calculation

2 Answers2

0

If you want want entry in DataContext/Database, you have to write changes to the DataContext / Database. See: update record with entity framework

To reset CaloryToday of the Goal you need to know, when. When the user starts the application you could check, if there is a calculation for the current day. If not, it is a new day and caloryToday should be reset.

Goal goal = (Goal of the current user)

DateTime currentDay = DateTime.Now.Date;
var date = GetCalculationByGoalId(goal.GoalID).Max(p => p.Date);
if(date < currentDate) {
    ResetGoal(goal);
}
Community
  • 1
  • 1
H.G. Sandhagen
  • 772
  • 6
  • 13
  • I want to change the date, when I debug it, in DataContext it didn't change anything. – Olivia Olga Clarissa Dec 18 '16 at 09:33
  • I was check the edited answer, but I want to know how to reset the value when the day is end. – Olivia Olga Clarissa Dec 18 '16 at 13:44
  • I am not sure about the meaning of your table columns. Is it right that both have the same primary key (goalid)? How many rows your tables have, one per (different) user, one per day? – H.G. Sandhagen Dec 18 '16 at 15:15
  • The goalId on Goals is the primary key. For the Calculations, it is the foreign key. For rows, one per (different) user. I have the update record with entity framework to, check this link : http://pastebin.com/raw/hPfv9Gn7. I am not sure where I should put the reset into the right place. – Olivia Olga Clarissa Dec 18 '16 at 15:19
0

Okay, so I got what I wanted to do. I changed every calorieToday and amount values on tables became 0.

public static void ResetItem()
    {
        using (var db = new GoalDataContext())
        {
            foreach (var item in db.Goals)
                item.calorieToday = 0;

            foreach (var item2 in db.Calculations)
            {                    
                item2.amount = 0;
                db.Calculations.Remove(item2);
            }                   

            db.SaveChanges();
        }
    }

Then on MainPage section, I write this code

        private void CalorieTracker_Loaded(object sender, RoutedEventArgs e)
    {
        DateTime currentTimeStamp = new DateTime();
        using (var db = new GoalDataContext())
        {
            foreach (var datenow in db.Calculations)
                currentTimeStamp = datenow.date;
        }
        DateTime currentNow = DateTime.Now;
        int changeDay = currentNow.Day;
        if (currentTimeStamp.Day != changeDay)
        {
            DataContextHelper.ResetItem();
        }

        //other stuff's here
    }

It already tested and it's work! After day is changes, calorieToday value will reset. Compare the day of date from table and now's day.