I am trying to use SQL Server CE on Windows Phone Mango. Here is my DataContext:
public class FooDataContext : DataContext
{
private static readonly string DB_CONNECTION_STRING = "Data Source=isostore:/foo.sdf";
// Pass the connection string to the base class.
public FooDataContext() : base(DB_CONNECTION_STRING)
{ }
// can this be a property?
public Table<Foo> Items;
public void ClearDatabase()
{
if (DatabaseExists())
{
DeleteDatabase();
}
}
public void EnsureDatabaseExists()
{
if (!DatabaseExists())
{
CreateDatabase();
SubmitChanges();
}
}
}
In the constructor of the main page, I call EnsureDatabaseExists(). If I’ve changed the schema, I also include a call to ClearDatabase():
//fooData.ClearDatabase();
fooData.EnsureDatabaseExists();
Foo‘s columns are int, double, and DateTime. When I run the app without the ClearDatabase() call, I get the following error as soon as I try to access fooContext.Items:
The specified table does not exist. [ Foo ]
What is going on here? If I freshly install the app, everything works fine.
The backing table structures for a Linq To SQL DataContext are only created when CreateDatabase() is called, or when you explicitly update the schema through the DatabaseSchemaUpdater.Execute() method.
I suspect what is happening in your case, is that you are adding a new entity type to your data model, however you are not actually updating the schema via DatabaseSchemaUpdater. This results in Linq To SQL generating a query which references a table which has not yet been created within the database engine.
I would recommend that you check out the DatabaseSchemaUpdater class if you want to be able to handle schema changes in your application.
http://msdn.microsoft.com/en-us/library/hh133477(v=VS.95).aspx