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



>我有一个由3列组成的数据库,分别是Id(int),Title(nvarchar)和Description(nvarchar)。我添加了一个新的列名more(nvarchar),并生成了一个新的数据库上下文来替换旧的数据库上下文。添加新列后,我无法运行我的应用程序。我错过了什么?谢谢

以下是错误消息:

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

额外:删除新列more(nvarchar)并重新生成新的数据库上下文以再次替换它后,它正常工作。这意味着它的背面是旧的,而不向表中添加新列。

下面是 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;
    }
}
}

我遇到了类似的问题。据我所知,问题是尝试针对现有数据库修改架构。就我而言,如果数据库已经存在,则代码不会初始化数据库。

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

。因此,架构更改未反映,导致在选择新列时出现 null 引用。我通过重新启动删除以前的数据库文件的手机模拟器来解决。

最新更新