Problem adding a record using LINQ to SQL

I am trying to add an entry to a SQLServer db table using LINQ to SQL in my WPF application, but always get an error message related to a missing directive. Usually intellisense gives a hint of such a problem, but not this time. Here is my code with all the 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; using 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 the "Add" extension method cannot be found that takes the first argument of the type "System.Data.Linq.Table" (if you lack a usage directive or a reference to assembly?)

Please, what could be wrong with my code?

+4
source share
3 answers

The correct syntax is:

 context.Meetings.InsertOnSubmit(meeting); 
+8
source

You should not receive a debugger error. You should get a compile-time error because the Table.Add method does not exist. Some books / blogs (see, in particular, the Scott Guthrie LINQ to SQL tutorial ) that came before LINQ to SQL was finalized will use this syntax, but it has been replaced with Table.InsertOnSubmit . Replace the code as follows:

 context.Meetings.InsertOnSubmit(meeting); 

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

+7
source

You need to add the System.Data.Linq statement.

0
source

All Articles