submitchanges方法-Windows Phone上的DuplicateKeyException

时间:2015-03-13 21:37:06

标签: linq-to-sql

我正在学习如何使用Linq以及ObservableCollection将数据插入数据库。 数据将插入到ObservableCollection中,但不会插入到数据库中。有人可以解释下面的代码是怎么回事。系统在submitchanges方法上抛出未处理的异常。请指教。

public void populateDates(DateTime theWeek)
{
     ObservableCollection<theSchedule> theDatesList = new ObservableCollection<theSchedule>();

    for (int i = 0; i < 7; i++)
    {
        theSchedule theShift = new theSchedule 
        {
           theDay = (theWeek.AddDays(i).ToString("dd/MM/yyyy")),
           theTime = (theWeek.AddHours(i).ToString("HH:mm")) + " - " + (theWeek.AddHours(i+8).ToString("HH:mm"))
        };

        theDatesList.Add(theShift);
        //MessageBox.Show(theWeek.AddDays(i).ToString("HH:mm"));
        shiftsDb.theSchedules.InsertOnSubmit(theShift);
    }
    shiftsDb.SubmitChanges();
    mylistbox.ItemsSource = theDatesList;
}

0 个答案:

没有答案