Xamarin sqlite mapping models to database table

1.9k Views Asked by At

I have deployed a database file for a xamarin project using the following guide. http://arteksoftware.com/deploying-a-database-file-with-a-xamarin-forms-app/

I created a model class as such:

[Table ("Person")]
public class Person  
{
    [PrimaryKey, AutoIncrement, Column("Id")]
    public int Id { get; set; }

    [NotNull, Column("Actor_Id")]
    public int ActorId { get; set; }
}

When I try to do an insert in the repository

dbConn.Insert(newPerson);

I'm getting SQLite.Net.SQLiteException: table Person has no column named ActorId.

If the column name in the database is Actor_Id shouldn't the [Column] attribute in the model map it to the table.

2

There are 2 best solutions below

1
On

There is a chance that your phone/emulator is previously deployed with app that with older database schema.

In this case, you will need to:

  • Remove the existing app in your phone/emulator. Then the next run should be OK due to it is created using new database schema. OR
  • Perform database upgrade script to change the schema of the current old database. Only do this if your app is already publish to store.
0
On

I found the problem to be that I was using the SQLite library in the model. Changing it to use SQLite.Net.Attributes allowed the correct mapping.

using SQLite.Net.Attributes;