views:

466

answers:

2

Hello, Guys Is it possible that I can add Table<T> dynamiclly to an exisitng DataContext Insance in LinqToSQL?

Here is my case: I have several classsed which are using [Test] attribute to declared as Table, I want to allow user to create the corresponding SQL Server tables during run-time. I understand that If I inherit the DataContext class, I can add member Table customers; in the class and it will automaticly create such a table at the backend database. However, the problem is that whether I can add Table during run-time to a DataContext class which help me to create the correspondgin SQL Server Table corresponding to T.

+1  A: 

I don't think that this is possible. Linq2Sql reads Meta information from your Attributes and store them in a cache - for all instances of your context.

The only chance you have is to generate those classes in Memory and then emit them into a new AppDomain. Sounds hard, but it isn't. But how do you want to access those classes? Reflection?

Arthur
A: 

You can just call GetTable<T>(); on the DataContext. It will read the attributes on T at that time.

Mike Two