简体   繁体   English

在Windows Phone的SQL Server Compact工具3.5中添加新列时出错

[英]Error when added a new column in SQL Server Compact tool 3.5 in Windows Phone

I have a database consist of 3 column which are Id(int), Title(nvarchar) and Description(nvarchar). 我有一个由3列组成的数据库,分别是Id(int),Title(nvarchar)和Description(nvarchar)。 I added a new column name more(nvarchar) and generated a new database context to replace the old one. 我添加了新的列名more(nvarchar)并生成了一个新的数据库上下文来替换旧的数据库上下文。 I am not able to run my application after adding a new column. 添加新列后,我将无法运行我的应用程序。 What am I missing? 我想念什么? Thanks 谢谢

Below is the error message: 下面是错误消息:

A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in Microsoft.Phone.Data.Internal.ni.dll

Extra: After removing the new column more(nvarchar), and regenerated a new database context to replace it again, it works as normal. 附加:删除新列more(nvarchar),并重新生成新的数据库上下文以再次替换它之后,它可以正常工作。 Meaning its back as the old one without adding a new column to the table. 意味着它的背面是旧的,而没有在表中添加新列。

Below is some code for the MainPage.xaml.cs: 以下是MainPage.xaml.cs的一些代码:

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

        // Set the data context of the LongListSelector control to the sample data
        DataContext = App.ViewModel;

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

    // Load data for the ViewModel Items
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }

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

    // Handle selection changed on LongListSelector
    private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        // If selected item is null (no selection) do nothing
        if (MLongListSelector.SelectedItem == null)
            return;

        //select the item selected from the class.property
        var title = (MLongListSelector.SelectedItem as PhoneApp.ToDoList).Title;
        var desc = (MLongListSelector.SelectedItem as PhoneApp.ToDoList).Description;
        var id = (MLongListSelector.SelectedItem as PhoneApp.ToDoList).Id;

        //send data through Title and Desc
        NavigationService.Navigate(new Uri("/ToDoDetailPage.xaml?Title=" + title + "&Desc=" + desc + "&Id=" + id, UriKind.Relative));


        // Navigate to the new page
        //NavigationService.Navigate(new Uri("/ToDoDetailPage.xaml", UriKind.Relative));

        // Reset selected item to null (no selection)
        MLongListSelector.SelectedItem = null;
    }

   private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var si = MainLongListSelector.SelectedItem as PhoneApp.ViewModels.ItemViewModel;

        if (MainLongListSelector.SelectedItem == null)
            return;

        if (si.LineOne.Equals("+ To Do List"))
            NavigationService.Navigate(new Uri("/todolistPage.xaml", UriKind.Relative));
        else if (si.LineOne.Equals("+ Reminder"))
            NavigationService.Navigate(new Uri("/reminderPage.xaml", UriKind.Relative));

        // Reset selected item to null (no selection)(//important)
        MainLongListSelector.SelectedItem = null;
    }

}
}

I ran into a similar issue. 我遇到了类似的问题。 As far as I could tell the issue is attempting to modify the schema against an existing database. 据我所知,问题是试图针对现有数据库修改架构。 In my case the code does not initialize the DB if it already exists. 就我而言,如果数据库已经存在,则代码不会对其进行初始化。

if (db.DatabaseExists() == false)
{
    // Create database.
    db.CreateDatabase();
}

...so the schema changes were not reflected, resulting in a null reference when selecting the new column. ...因此架构更改未反映出来,因此在选择新列时会导致引用为空。 I resolved by restarting the phone emulator which removed the previous db file. 我通过重新启动手机模拟器解决了问题,该手机模拟器删除了先前的数据库文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM