2010-02-04 28 views
1

我無法弄清楚在子表中插入新記錄的正確方法。不能插入新行到子表中提交更改在LINQ到SQL應用程序

應用程序中存在單個數據上下文,目標表(CustNotes)是名爲Customer的表的子表。 Customer和CustNotes之間有一對多關聯。 datacontext名稱是CustomerOrdersDataContext。

下面是我使用的代碼:

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     int newSeqNumber; 
     FindID = (int)lstCustomerNames.SelectedValue; 

       var CustNoteNum = 
        (from c in dbC.CustNotes       
        where c.CustomerID == FindID 
        select new 
        {c.NoteOrder}).Max(c => c.NoteOrder); 

     newSeqNumber = CustNoteNum + 1; 

     CustomerOrdersDataContext notes = new CustomerOrdersDataContext(); 

     CustNote newNote = new CustNote(); 

     newNote.Note = NewNote.Text; 
     newNote.NoteOrder = (byte)newSeqNumber; 
     newNote.CustomerID = FindID; 

     ***notes.CustNotes.Add(newNote);*** 
     notes.SubmitChanges(); 
    } 

錯誤涉及粗體,斜體線。那就是:

System.Data.Linq.Table」不包含定義‘添加’,沒有擴展方法‘添加’接受一個類型的第一個參數‘System.Data.Linq.Table’可能被發現(你是否缺少使用指令或裝配參考?)

任何人都會有幫助的線索。

回答

0

相反的:

notes.CustNotes.Add(newNote); 

你應該這樣做:

notes.CustNotes.InsertOnSubmit(newNote); 
+0

了不起的幫助。謝謝。 這不僅工作,但它也指出了另一個bug ... newNote.Note = NewNote.Text; 應該是 newNote.Note = newNoteText.Text; – 2010-02-04 18:37:07

相關問題