C# 无法添加主键列

C# 无法添加主键列,c#,sqlite,xamarin,C#,Sqlite,Xamarin,我有两门课,一门叫MainWindowViewModel,另一门叫Budget。我有一个MainPage.xaml,用于存储我的整个应用程序UI 我在预算类中设置主键时遇到问题。我已经创建了两个到SQLite的独立连接,一个名为conn,另一个名为bud。当我的应用程序运行时,它会立即在OnAppearing()上抛出一个异常,表示无法向预算添加主键正在出现 只显示用户保存到listview的信息 protected override void OnAppearing() {

我有两门课,一门叫
MainWindowViewModel
,另一门叫
Budget
。我有一个MainPage.xaml,用于存储我的整个应用程序UI

我在
预算
类中设置主键时遇到问题。我已经创建了两个到SQLite的独立连接,一个名为
conn
,另一个名为
bud
。当我的应用程序运行时,它会立即在
OnAppearing()上抛出一个异常,表示无法向
预算添加主键<代码>正在出现
只显示用户保存到listview的信息

protected override void OnAppearing()
        {
            base.OnAppearing();

            using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
                {
                    conn.CreateTable<MainWindowViewModel>();
                    var APRS = conn.Table<MainWindowViewModel>().ToList();
                    APRListView.ItemsSource = APRS;
                }

            using (SQLiteConnection bud = new SQLiteConnection(App.FilePath))
                {
                    bud.CreateTable<Budget>();
                    var Budgets = bud.Table<Budget>().ToList();
                    BudgetListView.ItemsSource = Budgets;
                }
        }
下面是预算代码

using System;
using SQLite;


namespace APRooved.ViewModels
{


    public class Budget : ViewBaseModel
    {

        [PrimaryKey, AutoIncrement]
        public int Budget_ID { get; set; }


        public string BudgetInstanceText 
        {

            get
            {
                return OutgoingProductName;
            }
        }

        public string BudgetInstanceDetail
        {

            get
            {
                return "Monthly Cost: £" + OutgoingMonthlyCostS;
            }
        }

        private string outgoingProductName { get; set; }
        public string OutgoingProductName
        {
            get => outgoingProductName;
            set
            {
                outgoingProductName = value;
                OnPropertyChanged("OutgoingProductName");
            }
        }


        private decimal outgoingMonthlyCost { get; set; }
        public decimal OutgoingMonthlyCost
        {
            get => outgoingMonthlyCost;
            set
            {

                outgoingMonthlyCost = value;
                OnPropertyChanged("outgoingMonthlyCost");
                outgoingMonthlyCostFin();
            }

        }

        private string outgoingMonthlyCostS { get; set; }
        public string OutgoingMonthlyCostS
        {
            get => outgoingMonthlyCostS;
            set
            {

                outgoingMonthlyCostS = value;
                OnPropertyChanged("outgoingMonthlyCostS");
            }

        }


        public void outgoingMonthlyCostFin()
        {

            outgoingMonthlyCostS = outgoingMonthlyCost.ToString();
        }

    }

}

最可能的原因是,
预算
已经存在,但没有PK列,并且无法修改现有表。删除应用程序应该可以解决这个问题,但您也可以尝试将
Budget
类重命名为
Budget2
,以测试该理论。

应该不需要两个不同的连接。如果错误明显存在于你的
预算中
类中,为什么你没有发布该代码?类是好的,它的连接不是(据我所知)如果你从设备/模拟器中删除你的应用并运行它,它能工作吗?@Jason no,我现在尝试了相同的连接名称conn。应用程序现在将加载,但每当我单击“删除”按钮时,它就会抛出异常“Budget not have a primary key”(预算没有主键)。最可能的原因是预算已经存在,没有PK列,并且无法修改现有表。删除应用程序应该可以解决这个问题,但是你也可以尝试将Budget类重命名为Budget2来测试这个理论。
using System;
using SQLite;


namespace APRooved.ViewModels
{


    public class Budget : ViewBaseModel
    {

        [PrimaryKey, AutoIncrement]
        public int Budget_ID { get; set; }


        public string BudgetInstanceText 
        {

            get
            {
                return OutgoingProductName;
            }
        }

        public string BudgetInstanceDetail
        {

            get
            {
                return "Monthly Cost: £" + OutgoingMonthlyCostS;
            }
        }

        private string outgoingProductName { get; set; }
        public string OutgoingProductName
        {
            get => outgoingProductName;
            set
            {
                outgoingProductName = value;
                OnPropertyChanged("OutgoingProductName");
            }
        }


        private decimal outgoingMonthlyCost { get; set; }
        public decimal OutgoingMonthlyCost
        {
            get => outgoingMonthlyCost;
            set
            {

                outgoingMonthlyCost = value;
                OnPropertyChanged("outgoingMonthlyCost");
                outgoingMonthlyCostFin();
            }

        }

        private string outgoingMonthlyCostS { get; set; }
        public string OutgoingMonthlyCostS
        {
            get => outgoingMonthlyCostS;
            set
            {

                outgoingMonthlyCostS = value;
                OnPropertyChanged("outgoingMonthlyCostS");
            }

        }


        public void outgoingMonthlyCostFin()
        {

            outgoingMonthlyCostS = outgoingMonthlyCost.ToString();
        }

    }

}