0

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; }
}
atiyar
  • 7,762
  • 6
  • 34
  • 75
Razvan Emil
  • 533
  • 9
  • 25
  • Which library you are using? sqlite-net-pcl does migration automatically. Please refer https://stackoverflow.com/a/39581486/8892050 – Ranjit Feb 21 '21 at 14:38
  • 3
    yes, try this lib https://www.nuget.org/packages/sqlite-net-pcl/ – Denys Kazakov Feb 21 '21 at 18:32
  • 1
    As per my understanding, no need to enable migration manually. By default it works. I have tested by adding a column for existing table. sqlite added new column with null values for existing data. If possible, please share your sample project. – Ranjit Feb 22 '21 at 04:41
  • 1
    @Ranjit i am using Microsoft.EntityFrameworkCore.Sqlite.Core at the moment, wondering if I can just make it work for this, instead of the sqlite-net-pcl nuget package. – Razvan Emil Feb 22 '21 at 20:52
  • 1
    @Ranjit, I was wrong earlier, I am not actually using the sqlite-net-pcl, but entity framework – Razvan Emil Feb 22 '21 at 21:04
  • 1
    @RazvanEmil, I have not used EntityFrameworkCore.Sqlite. So, no idea for automatic migration. I came across this url https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=vs. You would have referred already. If not, this may give you fair idea. – Ranjit Feb 23 '21 at 00:36

0 Answers0