Tutorial :LINQ to SQL - No Add method available



Question:

I have created a LINQ to SQL datacontext with a single datatable in it. I am trying to simply insert a new record into that table. The problem I am coming across is LINQ is not offering an Add method to pass in the new record to. I have seen countless examples where there is an Add method, but I can't seem to get it. Am I completely missing something or is it something else?

using (praetoriaTestDataContext db = new praetoriaTestDataContext())  {      PageHit hit = new PageHit();      hit.DateViewed = DateTime.Now;      hit.Page = "test";        db.PageHits.Add(hit); //Add method is not available!      db.SubmitChanges();  }  

Thanks!


Solution:1

Table's Add and Remove methods have been renamed to InsertOnSubmit and DeleteOnSubmit.

db.PageHits.InsertOnSubmit(hit);  


Solution:2

With LINQ-to-SQL, you want PageHits.InsertOnSubmit


Solution:3

I had a similar issue, but InsertOnSubmit was not appearing either. It turned out that I was missing a reference to the System.Data.LINQ Assembly.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »