Microsoft.Phone.Data.Internal.ni.dll中出现'System.Data.SqlServerCe.SqlCeException'类型的第一次机会异常

时间:2014-02-24 09:35:55

标签: c# sql-server windows-phone-7

我在数据库表中添加了一个新列。当我添加这个新列时,第一次机会异常开始出现。当我调试它时,它说明了代码行
MLongListSelector.ItemsSource = c.ToDoList.ToList(); 是导致问题的下一个执行语句。那我做错了什么?

using (DatabaseContext c = new DatabaseContext(DatabaseContext.ConnectionString))
        {
            c.CreateIfNotExists();
            c.LogDebug = true;
            //output todolist data from database
            MLongListSelector.ItemsSource = c.ToDoList.ToList();
        }

该行的堆栈跟踪:

    ex.StackTrace   "   at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)\r\n   at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()\r\n   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)\r\n   at System.Data.SqlServerCe.SqlCeCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult, Boolean isCompiledQuery)\r\n   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries, Boolean isCompiledQuery)\r\n   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)\r\n   at System.Data.Linq.Table`1.GetEnumerator()\r\n   at System.Data.Linq.Table`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at PhoneApp.MainPage.OnNavigatedTo(NavigationEventArgs e)"  

e.message:

    ex.Message  "The column name is not valid. [ Node name (if any) = t0,Column name = Col ]"   string

获取并设置:

public partial class ToDoList : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private string _Title;

    private string _Description;

    private string _Col;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIdChanging(int value);
partial void OnIdChanged();
partial void OnTitleChanging(string value);
partial void OnTitleChanged();
partial void OnDescriptionChanging(string value);
partial void OnDescriptionChanged();
partial void OnColChanging(string value);
partial void OnColChanged();
#endregion

    public ToDoList()
    {
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this.OnIdChanging(value);
                this.SendPropertyChanging();
                this._Id = value;
                this.SendPropertyChanged("Id");
                this.OnIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Title", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
    public string Title
    {
        get
        {
            return this._Title;
        }
        set
        {
            if ((this._Title != value))
            {
                this.OnTitleChanging(value);
                this.SendPropertyChanging();
                this._Title = value;
                this.SendPropertyChanged("Title");
                this.OnTitleChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Description", DbType="NVarChar(100)")]
    public string Description
    {
        get
        {
            return this._Description;
        }
        set
        {
            if ((this._Description != value))
            {
                this.OnDescriptionChanging(value);
                this.SendPropertyChanging();
                this._Description = value;
                this.SendPropertyChanged("Description");
                this.OnDescriptionChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col", DbType="NVarChar(25)")]
    public string Col
    {
        get
        {
            return this._Col;
        }
        set
        {
            if ((this._Col != value))
            {
                this.OnColChanging(value);
                this.SendPropertyChanging();
                this._Col = value;
                this.SendPropertyChanged("Col");
                this.OnColChanged();
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我刚试过你的解决方案而且我不明白它为什么不起作用。你有ToDoList标有“表属性”吗?即

[Table]
public partial class ToDoList : INotifyPropertyChanging, INotifyPropertyChanged

此外,您可能值得删除数据库并重新创建它。按照下面的代码示例尝试从那里开始。如果这不起作用,那么随意附上一个复制问题的简单项目。

<强>的Xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <ListBox x:Name="lstToDos">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}"></TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>

代码

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

   protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        using (DatabaseContext c = new DatabaseContext(DatabaseContext.ConnectionString))
        {
            c.CreateIfNotExists(); 

            //Add new records for debug sake: 
            var dateNow = DateTime.UtcNow.TimeOfDay.ToString();
            var newToDo = new ToDoList { Col = "Col" + dateNow, Description = "Some description" + dateNow, Title = "Some title" + dateNow };

            c.ToDoLists.InsertOnSubmit(newToDo);
            c.SubmitChanges();

            lstToDos.ItemsSource = c.ToDoLists.ToList();
        }
    }
}

数据上下文文件

public class DatabaseContext : DataContext
{
    public DatabaseContext(string connectionString)
    : base(connectionString)
    {

    }


    public Table<ToDoList> ToDoLists
    {
        get
        {
            return this.GetTable<ToDoList>();
        }
    }

    public void CreateIfNotExists()
    {
        if (!this.DatabaseExists())
        {
            this.CreateDatabase();
        }
    }

    public const string ConnectionString = @"isostore:/MyDatabases.sdf";
}

ToDO实体

 [Table]
public partial class ToDoList : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private string _Title;

    private string _Description;

    private string _Col;

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnIdChanging(int value);
    partial void OnIdChanged();
    partial void OnTitleChanging(string value);
    partial void OnTitleChanged();
    partial void OnDescriptionChanging(string value);
    partial void OnDescriptionChanged();
    partial void OnColChanging(string value);
    partial void OnColChanged();
    #endregion

    public ToDoList()
    {
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this.OnIdChanging(value);
                this.SendPropertyChanging();
                this._Id = value;
                this.SendPropertyChanged("Id");
                this.OnIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Title", DbType = "NVarChar(100) NOT NULL", CanBeNull = false)]
    public string Title
    {
        get
        {
            return this._Title;
        }
        set
        {
            if ((this._Title != value))
            {
                this.OnTitleChanging(value);
                this.SendPropertyChanging();
                this._Title = value;
                this.SendPropertyChanged("Title");
                this.OnTitleChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Description", DbType = "NVarChar(100)")]
    public string Description
    {
        get
        {
            return this._Description;
        }
        set
        {
            if ((this._Description != value))
            {
                this.OnDescriptionChanging(value);
                this.SendPropertyChanging();
                this._Description = value;
                this.SendPropertyChanged("Description");
                this.OnDescriptionChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Col", DbType = "NVarChar(25)")]
    public string Col
    {
        get
        {
            return this._Col;
        }
        set
        {
            if ((this._Col != value))
            {
                this.OnColChanging(value);
                this.SendPropertyChanging();
                this._Col = value;
                this.SendPropertyChanged("Col");
                this.OnColChanged();
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}