can't insert new row into a child table with submit changes in a linq to sql app
I can't figure out the correct way to insert a new record in a child table.
There's a single datacontext in the app and the target table (CustNotes) is a child of a table named Customer. There's a one to many association between Customer and Cust开发者_开发技巧Notes. The datacontext name is CustomerOrdersDataContext.
Here's the code I'm using:
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();
}
The error relates to the bold, italic line. Here it is:
System.Data.Linq.Table' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Data.Linq.Table' could be found (are you missing a using directive or an assembly reference?)
Anybody have a clue that'll help.
Instead of:
notes.CustNotes.Add(newNote);
You should do:
notes.CustNotes.InsertOnSubmit(newNote);
精彩评论