I am converting an application to ASP.Net Core that requires users to be able to work offline in certain instances. Previously, we handled this by copying data into a SQLite database, and downloading that data onto the client. I am running into some issues doing this within the context of ASP.NET Core/Entity Framework.
For context, I have a model that looks like this:
public class Facility
{
public Guid Id {get; set;}
public string Code {get; set;}
public string Description {get; set;}
}
The sql script for building the SQLite database looks like this:
CREATE TABLE [Facility] (
[Id] uniqueidentifier NOT NULL
, [Code] nvarchar(8) NOT NULL
, [Description] nvarchar(30) NULL
, CONSTRAINT [PK_Facility] PRIMARY KEY ([Id])
);
I am copying the data between the Sql database, and the SQLite database like so:
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
optionsBuilder.UseSqlite(MyConnectionString);
using (var sqliteContext = new MyContext(optionsBuilder.Options))
{
await sqliteContext.Fac.AddRangeAsync(CollectionOfFacilityObjects);
await sqliteContext.SaveChangesAsync();
}
This works well except for there being some trouble with the GUID
Id values being inserted into the SQLite database. Querying the SQLite database in SQLite Studio shows the problem with the values.
Im guessing this has to do with SQLite not actually supporting an official GUID
datatype, as seen here. Thus the value of the GUID
Id in my model classes is not translated correctly. Is there anyway that I can get the GUID
value to copy over correctly?
Note: My original approach to this problem was to generate insert statements for each row that needed to by copied over, and run them all in one transaction. This worked initially, but quickly became unfeasable when trying to insert >10k records or so.