LINQ (VS2008) - Add Method on DataContext not defined?
I have played around with LiNQ for a while now and I have been using the visual express editions until now. Today however, I installed VS2008 and tried to startup my LiNQ demo project for my manager. Suddenly I couldn’t Add new entries to various classes and the error VS2008 was giving me was:
“Error 1 ‘System.Data.Linq.Table<DBML.Player>’ does not contain a definition for ‘Add’ and no extension method ‘Add’ accepting a first argument of type ‘System.Data.Linq.Table<DBML.Player>’ could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\steve\My Documents\Visual Studio 2008\WebSites\GenLeague_new\Controls\Players\PlayerDetail.ascx.cs 61 42 C:\…\GenLeague_new\”
Now this confused me because the following code works perfectly in Visual Web Developer 2008:
newPlayerContext.Players.Add(_player);
In Visual Studio 2008 however, this line of code should read as follows:
newPlayerContext.Players.InsertOnSubmit(_player);
Why there is a difference between VS2008 and the express Web Developer Version is a mystery to me.
Blogged with Flock
very interesting, but I don’t agree with you
Idetrorce
Well, when i try to compile code with the .Add() method I get a compiler error, yet when I put .InsertOnSubmit() it works with no problems.
I would also like to know the answer to this inconsistency. I didn’t use web dev 08 for linq yet but many code examples had .Add which in VS08 wasn’t compiled into my DataContext class. (for Idetrorce)I am not really sure how you “don’t agree” unless you simply have some different version of VS08.
Anyway Steven thanks for the tip on the InsertOnSubmit() instead.
http://blogs.msdn.com/jongallant/archive/2007/12/01/linq-add-renamed-to-insertonsubmit.aspx
Hey Joe,
Thanks for the comments and the link! MS really should include this type of change in their newsletters. What is still weird that one method works in the Express editions and the other in VS. Strange!!!!
Thank you both Steve and Joe.
Glad to have helped Antonio
Hi. I’ve been hit by the same problem too. Thanks for the solution Steven.
But anyway, I still interested to know what really happen. Anybody can shed a light?
I have no idea, all the MVPs I have asked have no idea or wasn’t aware that there was a difference! I think we need to put this down to a MS change that just wasn’t correctly broadcast. These things happen though and as developers I think we should just roll with the changes and find the solutions as we encounter these small problems! Thanks for the comment
I am agree with you that Add method is renamed to InsertOnSubmit(). The other useful method is InsertAllOnSubmit() which accepts a list of records (objects).
I do not believe this