开发者

Problem with adding record using LINQ to SQL

开发者 https://www.devze.com 2022-12-15 01:29 出处:网络
I try to add a record to SQLServer db table using LINQ to SQL in my WPF app, but always get an error regarding missing directive. Usually intellisense gives a hint on such issue, but not this time. He

I try to add a record to SQLServer db table using LINQ to SQL in my WPF app, but always get an error regarding missing directive. Usually intellisense gives a hint on such issue, but not this time. Here my code with all directives:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
u开发者_运维技巧sing System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
...
...
 DateTime newdate = new DateTime(2010, 2, 1);
 TimeSpan newtime = new TimeSpan(12, 00, 00);    

        MyfirstdbDataContext context = new MyfirstdbDataContext();
        Meeting meeting = new Meeting();
        meeting.MeetID = "01.02.10_12:00";
        meeting.Date = newdate;
        meeting.Time = newtime;
        context.Meetings.Add(meeting); // Here I get the debugger error
        context.SubmitChanges();

And I get this error:

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?)

Please, what could be wrong with my code?


The correct syntax is:

context.Meetings.InsertOnSubmit(meeting);


You should not be getting a debugger error. You should be getting a compile-time error as there is not a Table.Add method. Some books/blogs (cf. especially Scott Guthrie's LINQ to SQL tutorial) that went to press before LINQ to SQL was finalized will use this syntax but it was replaced by Table.InsertOnSubmit. Replace your code by the following:

context.Meetings.InsertOnSubmit(meeting);

Here is the announcement that Add was renamed to InsertOnSubmit: LINQ: "Add" renamed to "InsertOnSubmit"


You need to add a System.Data.Linq using statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消