https://github.com/ivanmurzak/unity-efcore-sqlite
Bundle project that includes references on EntityFrameworkCore and SQLite set of packages to provide ready to go solution for Windows, MacOS, Android, iOS platforms.
https://github.com/ivanmurzak/unity-efcore-sqlite
efcore sqlite unity unity-engine unity-package unity-plugin
Last synced: 23 days ago
JSON representation
Bundle project that includes references on EntityFrameworkCore and SQLite set of packages to provide ready to go solution for Windows, MacOS, Android, iOS platforms.
- Host: GitHub
- URL: https://github.com/ivanmurzak/unity-efcore-sqlite
- Owner: IvanMurzak
- License: mit
- Created: 2024-12-11T09:31:39.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-03-31T12:07:13.000Z (about 2 months ago)
- Last Synced: 2025-05-01T23:45:45.715Z (28 days ago)
- Topics: efcore, sqlite, unity, unity-engine, unity-package, unity-plugin
- Language: C#
- Homepage:
- Size: 3.63 MB
- Stars: 13
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unity + EFCore + SQLite = ❤️
 [](https://openupm.com/packages/extensions.unity.bundle.efcore.sqlite/)  [](https://stand-with-ukraine.pp.ua)
Ready to go bundle package that includes references on [EntityFrameworkCore](https://github.com/dotnet/efcore) and [SQLitePCLRaw](https://github.com/ericsink/SQLitePCL.raw) packages that just works in this combination for the next platforms:
Supports AOT an JIT compilation. For AOT it uses nested `link.xml` file to exclude required classes from stripping.
## Supported project settings
### Platform
- ✔️ Windows
- ✔️ Android
- ✔️ iOS
- ✔️ MacOS
- Others not yet tested### Scripting backend
- ❌ `Mono`
- ✔️ `IL2CPP`### API Compatibility
- ❌ `.NET Framework`
- ✔️ `.NET Standard 2.0`
- ✔️ `.NET Standard 2.1`# Usage
Call the function once at app startup. Important to do that before opening SQLite connection.
```C#
SQLitePCLRaw.Startup.Setup();
```Then use EFCore as usual.
# Installation
- [Install OpenUPM-CLI](https://github.com/openupm/openupm-cli#installation)
- Open command line in Unity project folder
- Run the command``` CLI
openupm add extensions.unity.bundle.efcore.sqlite
```# 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](https://learn.microsoft.com/en-us/ef/ef6/get-started?redirectedfrom=MSDN). My favorite approach is `Code First`.
Please keep in mind. Because of Unity's .NET Standard 2.1 restrictions we are only limited to use the old version of EntityFrameworkCore 5.0.17. Newer versions require newer .NET version which Unity doesn't support yet. Anyway the version 5.0.17 is a good one for sure!