SQL Server CE 表未找到错误
我正在尝试在 Windows Phone Mango 上使用 SQL Server CE。这是我的 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();
}
}
}
在主页的构造函数中,我调用 EnsureDatabaseExists()
。如果我更改了架构,我还会调用 ClearDatabase()
:
//fooData.ClearDatabase();
fooData.EnsureDatabaseExists();
Foo
的列是 int
、double
和日期时间
。当我在没有 ClearDatabase()
调用的情况下运行应用程序时,当我尝试访问 fooContext.Items
时,我会收到以下错误:
指定的表不存在。 [ 福 ]
这是怎么回事?如果我新安装该应用程序,一切正常。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当调用 CreateDatabase() 或通过 DatabaseSchemaUpdater.Execute() 方法显式更新架构时,才会创建 Linq To SQL DataContext 的支持表结构。
我怀疑您的情况发生的是,您正在向数据模型添加新的实体类型,但您实际上并未通过 DatabaseSchemaUpdater 更新架构。这会导致 Linq To SQL 生成一个查询,该查询引用尚未在数据库引擎中创建的表。
如果您希望能够处理应用程序中的架构更改,我建议您查看 DatabaseSchemaUpdater 类。
http://msdn.microsoft.com/en-我们/库/hh133477(v=VS.95).aspx
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