简体   繁体   中英

System Exception: 'Cannot create a table without columns, does it have public properties?

I am creating a Xamarin form where I have to create a table to save details (user and password).

When I run this on emulator I end up with this error:

System Exception: cannot create table without columns

In VIEWS:

private async void _saveButton_Clicked(object Sender, EventArgs e)
{
    var db = new SQLiteConnection(_dbPath);

    db.CreateTable<User>();

    var maxPk = db.Table<User>().OrderByDescending(c => c.Id).FirstOrDefault();

    User user = new User()
        {
            Id = (maxPk == null ? 1 : Convert.ToInt32(maxPk.Id) +1),
            Email = _emailEntry.Text,
            Password = _passwordEntry.Text
        };

    db.Insert(user);
    await DisplayAlert(null, "Saved", "Ok");
    await Navigation.PopAsync();
}

In MODELS:

using SQLite;

namespace LoginPage.Models
{
    public class LoginPage
    {
        [PrimaryKey]
        public int Id { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }

        public override string ToString()
        {
            return this.Email + "," + this.Password;
        }
    }
}

In Models change the name of your class to User. That is the Table Model you are trying to create.

using SQLite;

namespace LoginPage.Models
{
    public class User
    {
        [PrimaryKey]
        public int Id { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }

        public override string ToString()
        {
            return this.Email + "," + this.Password;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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