如果LINQPAD没有保存更改的外键,我该怎么办? 以下代码不起作用:
var classScheduleToUpdate = ClassSchedules.First(x => x.Tp_classScheduleId == item.Tp_classScheduleId);
classScheduleToUpdate.Tp_classId = lastInsertedId;
SubmitChanges();
如果我将它复制到其他标签,硬编码一些ID并执行,那么一切正常。
完整代码如下。工作前后的一切都很好。
void Main()
{
var classSchedules = ClassSchedules.Where(x => x.Active).GroupBy(x => new { x.Tp_classId, x.DayOfWeek }).Where(x => x.Count() > 1).ToList();
foreach(var cs in classSchedules)
{
int i=0;
foreach(var item in cs)
{
if(i == 0) { i++; continue; }
var newClass = new Classes()
{
Active = true,
ActiveFrom = item.Classes.ActiveFrom,
ActiveTo = item.Classes.ActiveTo,
Color = item.Classes.Color,
Name = item.Classes.Name,
Recommended = item.Classes.Recommended,
ClassDescription = item.Classes.ClassDescription,
};
Classes.InsertOnSubmit(newClass);
SubmitChanges();
int lastInsertedId = newClass.Tp_classId;
var classScheduleToUpdate = ClassSchedules.First(x => x.Tp_classScheduleId == item.Tp_classScheduleId);
classScheduleToUpdate.Tp_classId = lastInsertedId;
SubmitChanges();
foreach(var reservation in item.Reservations)
reservation.Tp_classId = lastInsertedId;
foreach(var cancellation in item.ClassSchedules_Cancellations)
cancellation.Tp_classId = lastInsertedId;
foreach(var rating in item.ClassRatings)
rating.Tp_classId = lastInsertedId;
foreach(var cccs in item.ClientClasses_ClassSchedules)
cccs.Tp_classId = lastInsertedId;
foreach(var entrance in item.SingleClientClassEntrances)
entrance.Tp_classId = lastInsertedId;
SubmitChanges();
}
}
}