dimanche 12 juillet 2015

SQLite InsertWithChildren or UpdateWithChildren?

I have two tables and I am trying to insert records into the child table using InsertWithChildren, unfortunately it doesn't work as expected.

public class Schedule
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public DateTime ShiftStart { get; set; }
    public DateTime ShiftEnd { get; set; }
    public string ShiftNotes { get; set; }
    public string Company { get; set; }
    public string Color { get; set; }
    public string Visibility { get; set; }
    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<Punches> Punches { get; set; }
}

public class Punches
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [ForeignKey(typeof(Schedule))]
    public int ScheduleId { get; set; }
    public string Company { get; set; }
    public DateTime Date { get; set; }
    public DateTime Time { get; set; }
    public string Mode { get; set; }
    //public TimeSpan Duration { get; set; }

}

These are the 2 tables. How the insertion happen is, first I insert a record onto the Schedules table then I add the punches for that record later. I am able to insert the record onto the Schedules tables and later when I add the punches which is related to that record on the schedules table, it just gets overwritten, it doesn't add to it. This is the way I add records to the Schedules Table:

Schedule newSchedule = new Schedule()
                {
                    Date = Convert.ToDateTime(tblDate.Text),
                    ShiftStart = Convert.ToDateTime(btnShiftStart.Content.ToString()),
                    ShiftEnd = Convert.ToDateTime(btnShiftEnd.Content.ToString()),
                    ShiftNotes = tbAddNotes.Text,
                    Company = App.company,
                    Color = App.color,
                    Visibility = "Collapsed",
                    //Punches = new List<Punches>({,
                };
                App.db.InsertWithChildren(newSchedule, true);

And this way to the Punches table:

var sch = App.db.Find<Schedule>(s => s.Id == theSelectedShift.Id);
                List<Punches> newPunch = new List<Punches>();
                newPunch.Add(new Punches()
                    {
                        Company = theSelectedShift.Company,
                        Date = theSelectedShift.Date,
                        Time = DateTime.Now,
                        Mode = mode,
                    });
                sch.Punches = newPunch;
                App.db.InsertWithChildren(sch);

Please advise!

Aucun commentaire:

Enregistrer un commentaire