Skip to content

Bundle project that includes references on EntityFrameworkCore and SQLite set of packages to provide ready to go solution for Windows, MacOS, Android, iOS platforms.

License

Notifications You must be signed in to change notification settings

IvanMurzak/Unity-EFCore-SQLite

Repository files navigation

Unity + EFCore + SQLite = ❤️

npm openupm License Stand With Ukraine

Ready to go bundle package that includes references on EntityFrameworkCore and SQLitePCLRaw 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.

SQLitePCLRaw.Startup.Setup();

Installation

openupm add extensions.unity.bundle.efcore.sqlite

Manual Installation (alternative)

Modify /Packages/manifest.json file by adding requierd depedencies and scopedRegistries

{
  "dependencies": {
    "extensions.unity.bundle.efcore.sqlite": "0.0.4",
    "org.nuget.castle.core": "5.1.1",
    "org.nuget.sqlitepclraw.bundle_e_sqlite3": "2.1.10",
    "org.nuget.sqlitepclraw.provider.e_sqlite3": "2.1.10",
    "org.nuget.sqlitepclraw.provider.sqlite3": "2.1.10",
    "org.nuget.sqlitepclraw.lib.e_sqlite3": "2.1.10"
  },
  "scopedRegistries": [
    {
      "name": "package.openupm.com",
      "url": "https://package.openupm.com",
      "scopes": [
        "com.openupm",
        "extensions.unity.bundle.efcore.sqlite"
      ]
    },
    {
      "name": "unitynuget-registry.azurewebsites.net",
      "url": "https://unitynuget-registry.azurewebsites.net",
      "scopes": [
        "org.nuget"
      ]
    }
  ]
}

Alternative usage

1. Create SQLiteContext class

using Microsoft.EntityFrameworkCore;

public class SQLiteContext : DbContext
{
    // sample table of levels in your database
    public DbSet<LevelData> Levels { get; set; }

    public SQLiteContext() : base() { }
    public SQLiteContext(DbContextOptions<SQLiteContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.Entity<LevelData>();
    }
}

2. Create SQLiteContextFactory class

using Microsoft.EntityFrameworkCore;

public class SQLiteContextFactory : EFCoreSQLiteBundle.SQLiteContextFactory<SQLiteContext>
{
    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<SQLiteContext> 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

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. 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!

About

Bundle project that includes references on EntityFrameworkCore and SQLite set of packages to provide ready to go solution for Windows, MacOS, Android, iOS platforms.

Topics

Resources

License

Stars

Watchers

Forks