Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms, Sqlite, EF Core 3, migrations, and lot of confussion

I have a Xamarin Forms app that I have switched from using only restful API to using local SQLite DB, that will sync using this Dotmim.Sync - DB Sync'ing Framework (which is great!). I am using EF Core 3 in the Xamarin forms project to interact with SQLite.

My questions are around running migrations or just database updates on SQLite. I have read several blogs and forums about different approaches, but they all are several years old, from EF Core 1 to EF Core 2, and lots of work around.

  1. https://forums.xamarin.com/discussion/101805/xamarin-android-entity-framework-core-2-and-migrations - this talks about running migrations by add console application
  2. https://medium.com/@yostane/entity-framework-core-and-sqlite-database-migration-using-vs2017-macos-28812c64e7ef - this add's a Dot Net Cli Tool Reference. I tried this and got errors, but it also EF Core 1.1
  3. https://www.algoworks.com/blog/xamarin-forms-and-entity-framework-core/ - gave me more questions then answers.

There are more links but I, these prove my point of all the different approaches taken.

What I am trying to achieve is:

  1. When a user opens the app, it checks if there is DB version update, or the app updated and knows it has to update the database.
  2. When there is a DB update, Dotmim.Sync Framework is in control of Provisioning and Deprovisioning the database on the client (Xamarin Forms). Which gives the opportunity to run EFCore Migrations or SQL Sricpts to update the SQLite store.

Option 1 I would like to go the EF Core Migration path, since the Master DB is updated with Migrations, The App would be able to leverage the same scripts. If running migrations on Xamarin forms at runtime is possible that would be great.

Q1. Can EF Core run mirgations in Xamarin Forms apps during runtime, same as it would in .netcore/netframework project?

Option 2 If running scripts needs to be the path thats fine, which i have explored and means I need to be able to SQLite schema compares Tool 1 and tool 2, I tried the tool one on SQLite db3 and it error'ed. Wasn't willing to pay for Tool 2 unless without verification it works.

Q2. Is there a SQLite compare tool to generate schema changes?

Q3. Or what is database update path for sqlite deployed on clients, that would be better, i.e drop the DB and just recreate it? then re sync the DB, to load back all user specific data. The chances of lost data would be minimal, the sync logic in the App will be frequent, not sure after every user data change event, but possible if we need to make sure the server never out of sync.

I hope I made this clear. Thanks

like image 380
ChampChris Avatar asked Mar 06 '20 16:03

ChampChris


1 Answers

You'll need to create the dummy console app as mentioned. Here are instruction for installing the tools. After that, the normal Migrations workflow should work on Xamarin (Be sure to let us know if it doesn't).

Add-Migration MyMigration -P MyNetStandardClassLibrary -S DummyNetCoreApp

You can apply migrations at runtime using this method:

myDbContext.Database.Migrate();
like image 89
bricelam Avatar answered Nov 06 '22 18:11

bricelam