我有两个表,我试图使用InsertWithChildren将记录插入到子表中,遗憾的是它没有按预期工作。
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; }
}
这是2个表格。插入是如何发生的,首先我将一条记录插入到Schedules表中,然后我在稍后添加该记录的打孔。我可以将记录插入到Schedules表中,稍后当我在schedule表上添加与该记录相关的打孔时,它只会被覆盖,它不会添加到它。 这是我将记录添加到计划表的方式:
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);
通过这种方式进入Punches表:
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,
});
App.db.InsertAllWithChildren(newPunch);
sch.Punches = newPunch;
App.db.UpdateWithChildren(sch);
这里发生的是,它将数据作为Schedules表中现有记录的子项插入Punches表。第二次当我将另一行添加到Punches表中时,它也是Schedules表中同一行的子项,当我用GetWithChildren方法提取它时,我没有看到作为子记录添加的2条记录。 / p>
请指教!