In my Xamarin project I am using the
Microsoft.EntityFrameworkCore.Sqlite.Core (version 3.1.9 at the moment)
package.I also have the sqlite-net-pcl but I prefer a solution for using Entity framework core sqlite instead.
This is what I am using: https://learn.microsoft.com/en-us/ef/core/get-started/xamarin.
Need a solution that works on all platforms (iOS, Android, UWP).
Whenever I change the sqlite database in my Xamarin mobile project, by adding a column, or adding a table(in this case by adding a Transaction table), renaming a table/column etc. I get an exception of this sort when running over an existing installation and trying to do a PutTransaction (Transaction t) (which makes sense since I modified the local db):
SQLite Error 1: 'no such table: Transaction'. (this would disappear if the tester erases their whole app installation and reinstalls with a fresh db file)
How can I be able to change the DB structure when submitting a new build for testing, without the tester having to erase the whole app installation(and db file) because of the db being modified?
Is there any way in Xamarin to automatically implement MIGRATIONS in Sqlite automatically for this particular sqlite nuget package?
This my db context code:
public class MyMobileAppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Transaction> Transactions { get; set; }
public MyMobileAppDbContext()
{
SQLitePCL.Batteries_V2.Init();
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string dbPath = Path.Combine(FileSystem.AppDataDirectory, AppConstants.Constants.DatabaseFilename);
optionsBuilder
.UseSqlite($"Filename={dbPath}");
}
}
The User table:
public class User
{
public User()
{
Created = DateTime.Now;
}
[PrimaryKey]
public Guid Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Created { get; set; }
public string HashedPassword { get; set; }
}
And suppose a secondary table added later called Transaction class:
public class Transaction
{
[PrimaryKey]
public Guid Id { get; set; }
public DateTime PerformedOn { get; set; }
public string Type { get;set; }
}