I'm trying to create a WP7 application using MVVM Light and SQL Server CE as a database. To accomplish this I generated a datacontext using sqlmetal.exe (1).
This works ok if I run the application on my phone. But when trying to display design time data, I can't create an instance of the DataContext
in my design time code. Can this be done? Another way I tried was to create an instance of Table<MyDataObject>
, but System.Data.Linq.Table has no constructors.
So my question is if it is possible to somehow create an instance of my DataContext
from code or that there is another way to interface with my database without loosing design time data.
(1) Actually I used a different method using a library project to be able to visually design and copied the designer file to my WP7 project. The result in the same if I'm correct.
After many failed attempts I couldn't work out a way to programmatically mock the DataContext
object. So I did it in a different way solving my problem.
I created a service interface to query the data from the database. For every table in the SQL CE database I had to manually create a function.
public interface IPayDataService {
IQueryable<Account> GetAccounts();
IQueryable<User> GetUsers();
IQueryable<PayEntry> GetEntries();
}
In the real data object the corresponding tables are passed. In the design time object I create the objects in code and return the IQueryable
via AsQueryable()
. By using IQueryable
I make sure the queries stay optimised (in this case by Linq2Sql).
Though this is less then ideal and requires more manual code writing, I managed to get the design time data working again.