sql ce上不存在指定的表

时间:2012-01-02 14:23:55

标签: windows-phone-7 sql-server-ce windows-phone-7.1

我得到了一个例外

The specified table does not exist [Limits]

我正在尝试保存新项目

 (App.Current as App).context.Limits.InsertOnSubmit(new Limit() { Date = DateTime.Now, Value = inputLimit });//this works

 (App.Current as App).context.SubmitChanges();//here I get exception

此行我也收到错误:

 var currentLimit = (App.Current as App).context.Limits.Where(l => l.Date.Date == DateTime.Now.Date).FirstOrDefault();

这是一个“模特”

public class CalCounterContext:DataContext
{
    public CalCounterContext(string connstring):base(connstring)
    {
    }

    public Table<Limit> Limits;
    public Table<Meal> Meals;

}

[Table]
public class Limit
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "Int NOT NULL IDENTITY", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int Id { get; set; }

    [Column]
    public DateTime Date { get; set; }

    [Column]
    public int Value { get; set; }
}

有时它有效,有时则不然。什么可能是我的问题的共鸣?

2 个答案:

答案 0 :(得分:3)

当您在数据库的更高版本中添加表然后正在使用的表时,通常会发生这种情况。创建数据库上下文时,请检查它是否是最新的,如果不是,请使用Database described here将DatabaseSchemaUpdater类更新为数据库。如果您正在创建应用程序,请卸载并重新安装该应用程序。

此外,我遇到了一个奇怪的问题,即使应用程序在生产中没有任何推理,间歇性地我也会收到此错误。当我启动应用程序然后点击主页或后退按钮以快速结束时,通常会发生这种情况。我最终重新实现了GetTable&lt;&gt;用于在基础数据库类中实例化我的ITable变量的函数,以便它可以检查该表是否确实存在:

public Table<TEntity> VerifyTable<TEntity>() where TEntity : class
{
    var table = GetTable<TEntity>();
    try
    {
        // can call any function against the table to verify it exists
        table.Any();
    }
    catch (DbException exception)
    {
        if (exception.Message.StartsWith("The specified table does not exist."))
        {
            var databaseSchemaUpdater = this.CreateDatabaseSchemaUpdater();
            databaseSchemaUpdater.AddTable<TEntity>();
            databaseSchemaUpdater.Execute();
        }
        else
        {
            throw;
        }
    }
    return table;
}

答案 1 :(得分:0)

我遇到了同样的间歇性错误。尝试从设备中删除数据库并再次安装应用程序。我发现我的问题是由于我正在对模型进行更改而造成的,当我重新运行应用程序时,我会收到此错误。