advanced web statistics

Add & Remove GetTable Extension Methods Gone in 2008?

2/14/2008 11:35:59 AM

Moving from Beta 2 to the full version of Visual Studio 2008 recently coupled with Charlie Calvert's presentation last night motivated me to revisit some sandbox 3.5 projects.

One of the problems I first saw with LINQ was using multiple databases in one project.  This would mean that for each Database you wanted to use, you would have to generate LINQ to SQL classes for each and instantiate the appropriate DataContext class when wanting to CRUD, etc..  Well, you will still have to generate the LINQ to SQL classes but can now have access to the data from one Generic class (DataAccess<TDatabase> where TDatabase has contraints of DataContext and new()).  My first sandbox project with 2008 was a very incomplete DataAccess class library (hey it was the first week Beta 2 was released!). Here are two methods in question from that codebase which are slightly altered.

public static void Insert<T>(T instance) where T : class
{
   using (TDatabase database = new TDatabase())
   {
      database.GetTable<T>().Add(instance);
      database.SubmitChanges();
   }
}

public static void Delete<T>(Expression<Func<T, bool>> predicate)
where T : class
{
   using (TDatabase database = new TDatabase())
   {
      T t = (T)database.GetTable<T>().Where<T>(predicate).Single();
      database.GetTable<T>().Remove(t);
      database.SubmitChanges();
   }
}

IRL these are bools but I just pasted this for concept.  Anyway, when I compile this now I get compile-time errors tellng me that System.Data.Ling.Table<T> does not contain a definition for Add or Remove. I thought that these were native extension methods? Apparently things started being done differently between Beta 2 and the release. Unfortunately I wasn't able to keep up with all of this as I, like most, still had 2.0 work to do!

I do know that from CTP to Beta 2 Generic methods like above needed to have parameter-type constraints in order to compile.  I clearly have that!  At this point I am open to suggestions.  If I find anything I'll be sure to update this.

UPDATE
That didn't take long! Apparently the RTM bits consisted of refactoring (amongst other things) Add and Remove to InsertOnSubmit and DeleteOnSubmit respectively.  Also, apparently Add and Remove will still compile in VS 2008 Visual Web Developer Express. Interesting...

NEWLY-MODIFIED CODE

public static void Insert<T>(T instance) where T : class
{
   using (TDatabase database = new TDatabase())
   {
      database.GetTable<T>().InsertOnSubmit(instance);
      database.SubmitChanges();
   }
}

public static void Delete<T>(Expression<Func<T, bool>> predicate)
where T : class
{
   using (TDatabase database = new TDatabase())
   {
      T t = (T)database.GetTable<T>().Where<T>(predicate).Single();
      database.GetTable<T>().DeleteOnSubmit(t);
      database.SubmitChanges();
   }
}

C#, LINQ

kick it on DotNetKicks.com

Leave a Comment

   

  Enter the text to proceed!