0

I am building a library for Windows Phone 8 which requires local databases. Here is my understanding of how the LINQ-to-SQL works and creates database:

  • DataContext object is created from the corresponding class.
  • When CreateDatabase() method is called, it reads the connection string and the members of type Table from the DataContext object.
  • The method creates database at given location and creates tables corresponding to the members of DataContext object.

Now, the DataContext class has to be hard typed. As I am building a library, I wouldn't know which tables the user will need. Obviously, I cannot use the hard typed DataContext. Moreover, CreateTable() method doesn't exist in this scenario. If I simply start using GetTable method, I get Table does not exist error.

The question is, how do I get create tables without using a hard typed DataContext?

P.S.: My situation is sort of similar to this.

3
  • I think this is the closes you can get: stackoverflow.com/q/582221/861716 Commented Sep 29, 2013 at 21:07
  • @Gret Thanks for pointing that out. Doesn't seem to suite my scenario though. I don't even know if Windows Phone has enough freedom. I am thinking of accepting whole DataContext from the user of library before issuing call to CreateDatabase() method. Would that work? Commented Sep 30, 2013 at 4:51
  • I think it will not be possible Commented Sep 30, 2013 at 6:37

1 Answer 1

0

In the scenario of LINQ-to-SQL for Windows Phone, it is not possible to add tables to the DataContext after it is initialized. In my case, solution was to accept the whole DataContext from the user of library as follows:

First, you provide a base class from which users can derive.

public class DataContextBase : DataContext
{
    public DataContextBase(string connectionString) : base(connectionString)
    { }

    //Tables for library's internal workings.
    public Table<SyncTimeStamp> SyncTimeStamps;
}

Then user can pass the derived class while configuring the library. Library may accept this is method parameter:

public async void ConfigureSQLCE<T>(T mainDataContext) where T : DataContextBase
{   
    //Do whatever with it. MainDB is a dynamic here!
    MainDB = mainDataContext;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.