Bundle project that includes references on EntityFrameworkCore and SQLite set of packages to provide ready to go solution for Windows, MacOS, Android, iOS platforms.
link.xml file to exclude required classes from stripping.
Mono
IL2CPP
.NET Framework
.NET Standard 2.0
.NET Standard 2.1
C#
SQLitePCLRaw.Startup.Setup();
`
Then use EFCore as usual.
Installation
- Install OpenUPM-CLI
- Open command line in Unity project folder
- Run the command
` CLI
openupm add org.nuget.castle.core@5.1.1 --registry https://unitynuget-registry.openupm.com/
openupm add org.nuget.microsoft.entityframeworkcore@5.0.17 --registry https://unitynuget-registry.openupm.com/
openupm add org.nuget.microsoft.entityframeworkcore.abstractions@5.0.17 --registry https://unitynuget-registry.openupm.com/
openupm add org.nuget.microsoft.entityframeworkcore.analyzers@5.0.17 --registry https://unitynuget-registry.openupm.com/
openupm add org.nuget.microsoft.entityframeworkcore.relational@5.0.17 --registry https://unitynuget-registry.openupm.com/
openupm add org.nuget.microsoft.entityframeworkcore.sqlite.core@5.0.17 --registry https://unitynuget-registry.openupm.com/
openupm add org.nuget.sqlitepclraw.bundle_e_sqlite3@2.1.10 --registry https://unitynuget-registry.openupm.com/
openupm add org.nuget.sqlitepclraw.provider.sqlite3@2.1.10 --registry https://unitynuget-registry.openupm.com/
`
- Modify /Packages/manifest.json file by adding required dependencies and scopedRegistries
`json
{
"dependencies": {
"extensions.unity.bundle.efcore.sqlite": "0.0.9"
},
"scopedRegistries": [
{
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": [
"com.openupm"
]
}
]
}
`
Alternative usage
1. Create
SQLiteContext class
`C#
using Microsoft.EntityFrameworkCore;
public class SQLiteContext : DbContext
{
// sample table of levels in your database
public DbSet Levels { get; set; }
public SQLiteContext() : base() { }
public SQLiteContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity();
}
}
`
2. Create
SQLiteContextFactory class
`C#
using Microsoft.EntityFrameworkCore;
public class SQLiteContextFactory : EFCoreSQLiteBundle.SQLiteContextFactory
{
protected override string DesignTimeDataSource => "replace it with path to design time database";
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db") { }
protected override SQLiteContext InternalCreateDbContext(DbContextOptions optionsBuilder)
=> new SQLiteContext(optionsBuilder);
}
`
The EFCoreSQLiteBundle.SQLiteContextFactory class under the hood executes SQLitePCLRaw.Startup.Setup(); for proper SQLite setup depends on the current platform.
3. Create database context
`C#
var contextFactory = new SQLiteContextFactory();
var dbContext = contextFactory.CreateDbContext();
// use it for data manipulations
// sample:
var level_1 = dbContext.Levels.FirstOrDefault(level => level.id == 1);
`
Helpful information
Read more how to use EntityFrameworkCore. My favorite approach is Code First`.