I try to experiment with new platform for me, Xamarin Forms. Based on .Net Core and EF Core knowledge I decided to begin with injecting Sqlite ORM service (sqlite-net-pcl) in Xamarin Forms Shell template included in Visual Studio 2019. In this template there is already implemented Mock CRUD service based on in-memory data structure so I wanted to implement my own service and inject it with DependencyService. At first I have modified data model with needed attribute:
public class Item
{
[PrimaryKey, AutoIncrement]
public string Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }
}
Next I implemented CRUD service:
public class SqliteDataStore : IDataStore<Item>
{
private readonly SQLiteConnection _db;
public SqliteDataStore()
{
_db = new SQLiteConnection(Path.Combine(FileSystem.AppDataDirectory, "items.sqlite"));
_db.CreateTable<Item>();
if (_db.Table<Item>().Count().Equals(0))
{
_db.InsertAll(new List<Item>
{
new Item { Id = Guid.NewGuid().ToString(), Text = "First item", Description = "This is the first item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Second item", Description = "This is the second item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Third item", Description = "This is the third item description." }
}
);
}
}
public async Task<bool> AddItemAsync(Item item)
{
_db.Insert(item);
return await Task.FromResult(true);
}
public async Task<bool> DeleteItem(string id)
{
_db.Delete<Item>(id);
return await Task.FromResult(true);
}
public async Task<Item> GetItemAsync(string id)
{
return await Task.FromResult(_db.Get<Item>(id));
}
public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
{
return await Task.FromResult(_db.Table<Item>().ToList());
}
public async Task<bool> UpdateItemAsync(Item item)
{
_db.Update(item);
return await Task.FromResult(true);
}
}
Next I changed injected service in App class:
public App()
{
InitializeComponent();
DependencyService.Register<SqliteDataStore>();
MainPage = new AppShell();
}
This implementation properly works with EF Core in Xamarin Forms but EF Core is very slow so I changed ORM (sqlite-net-pcl) and it does not work.
In the document of dependency servicee, it says:
So, I think you have added the line
DependencyService.Register<SqliteDataStore>();
at a wrong place.Solution:
For example, in iOS it should be:
In Android:
There is also documents about Store Data in a Local SQLite.NET Database and Using SQLite.NET with Android you can refer.