bgcode
bgcode

Reputation: 121

Cannot add a PRIMARY KEY column

I have 2 classes one called MainWindowViewModel & one called Budget. I have a MainPage.xaml that stores my whole UI for the app.

I'm having an issue with setting a primary key in my Budget class. I have created 2 seperate connections to SQLite one called conn & the other called bud. When my App runs it instantly throws an exception on OnAppearing() saying cannot add a primary key to Budget. OnAppearing Just shows information the user has saved to a 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;
                }
        }

Below is my App.Xaml.cs

public App()
        {
            InitializeComponent();

            MainPage = new MainPage();
        }

        public App(string filePath)
        {
            InitializeComponent();

            MainPage = new MainPage();

            FilePath = filePath;
        }

code for Budget below...

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();
        }

    }

}

Upvotes: 0

Views: 1304

Answers (1)

Jason
Jason

Reputation: 89127

the most likely cause is that Budget already exists without a PK column, and it is unable to modify the existing table. Deleting the app should fix that, but you could also try renaming the Budget class to Budget2 to test that theory.

Upvotes: 1

Related Questions