无法在 linq to sql 应用程序中提交更改的情况下将新行插入到子表中
我无法找出在子表中插入新记录的正确方法。
应用程序中有一个数据上下文,目标表 (CustNotes) 是名为 Customer 的表的子表。 Customer 和 CustNotes 之间存在一对多关联。数据上下文名称是 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' 不包含“Add”的定义,并且找不到接受“System.Data.Linq.Table”类型的第一个参数的扩展方法“Add”(您是否缺少使用指令还是程序集引用?)
任何人都有可以提供帮助的线索。
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 CustNotes. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是:
你应该这样做:
Instead of:
You should do: