Skip to content

Commit e533bec

Browse files
committed
Updated dependencies and core implementation.
1 parent 2ab9a91 commit e533bec

File tree

17 files changed

+328
-249
lines changed

17 files changed

+328
-249
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@
5656
"temp/": true,
5757
"Temp/": true
5858
},
59-
"dotnet.defaultSolution": "Unity-EntityFrameworkCore-SQLite.sln"
59+
"dotnet.defaultSolution": "Unity-EntityFrameworkCore-SQLite.sln",
60+
"cSpell.words": [
61+
"efcore"
62+
]
6063
}

Assets/_PackageRoot/Documentation~/README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Ready to go bundle package that includes references on [EntityFrameworkCore](htt
77
- ✔️ Windows
88
- ✔️ Android
99
- ✔️ iOS
10-
- MacOS (need to test)
10+
- MacOS (not yet tested)
11+
12+
Supports AOT an JIT compilation. For AOT it uses `link.xml` file to exclude required classes from stripping.
1113

1214
# Usage
1315

@@ -26,3 +28,60 @@ SQLitePCLRaw.Startup.Setup();
2628
``` CLI
2729
openupm add extensions.unity.bundle.efcore.sqlite
2830
```
31+
32+
# Alternative usage
33+
34+
## 1. Create `SQLiteContext` class
35+
36+
```C#
37+
using Microsoft.EntityFrameworkCore;
38+
39+
public class SQLiteContext : DbContext
40+
{
41+
// sample table of levels in your database
42+
public DbSet<LevelData> Levels { get; set; }
43+
44+
public SQLiteContext() : base() { }
45+
public SQLiteContext(DbContextOptions<SQLiteContext> options) : base(options) { }
46+
47+
protected override void OnModelCreating(ModelBuilder builder)
48+
{
49+
base.OnModelCreating(builder);
50+
builder.Entity<LevelData>();
51+
}
52+
}
53+
```
54+
55+
## 2. Create `SQLiteContextFactory` class
56+
57+
```C#
58+
using Microsoft.EntityFrameworkCore;
59+
60+
public class SQLiteContextFactory : EFCoreSQLiteBundle.SQLiteContextFactory<SQLiteContext>
61+
{
62+
protected override string DesignTimeDataSource => "replace it with path to design time database";
63+
64+
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db") { }
65+
66+
protected override SQLiteContext InternalCreateDbContext(DbContextOptions<SQLiteContext> optionsBuilder)
67+
=> new SQLiteContext(optionsBuilder);
68+
}
69+
```
70+
71+
The `EFCoreSQLiteBundle.SQLiteContextFactory` class under the hood executes `SQLitePCLRaw.Startup.Setup();` for proper SQLite setup depends on the current platform.
72+
73+
## 3. Create database context
74+
75+
```C#
76+
var contextFactory = new SQLiteContextFactory();
77+
var dbContext = contextFactory.CreateDbContext();
78+
79+
// use it for data manipulations
80+
// sample:
81+
var level_1 = dbContext.Levels.FirstOrDefault(level => level.id == 1);
82+
```
83+
84+
# Helpful information
85+
86+
Read more how to use [EntityFrameworkCore](https://learn.microsoft.com/en-us/ef/ef6/get-started?redirectedfrom=MSDN). My favorite approach is `Code First`.
87+
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!

Assets/_PackageRoot/Plugins/SQLitePCLRaw.provider.sqlite3.2.1.10/netstandard2.0.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Assets/_PackageRoot/Plugins/SQLitePCLRaw.provider.sqlite3.2.1.10/netstandard2.0/SQLitePCLRaw.provider.sqlite3.dll.meta

Lines changed: 0 additions & 33 deletions
This file was deleted.

Assets/_PackageRoot/README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Ready to go bundle package that includes references on [EntityFrameworkCore](htt
77
- ✔️ Windows
88
- ✔️ Android
99
- ✔️ iOS
10-
- MacOS (need to test)
10+
- MacOS (not yet tested)
11+
12+
Supports AOT an JIT compilation. For AOT it uses `link.xml` file to exclude required classes from stripping.
1113

1214
# Usage
1315

@@ -26,3 +28,60 @@ SQLitePCLRaw.Startup.Setup();
2628
``` CLI
2729
openupm add extensions.unity.bundle.efcore.sqlite
2830
```
31+
32+
# Alternative usage
33+
34+
## 1. Create `SQLiteContext` class
35+
36+
```C#
37+
using Microsoft.EntityFrameworkCore;
38+
39+
public class SQLiteContext : DbContext
40+
{
41+
// sample table of levels in your database
42+
public DbSet<LevelData> Levels { get; set; }
43+
44+
public SQLiteContext() : base() { }
45+
public SQLiteContext(DbContextOptions<SQLiteContext> options) : base(options) { }
46+
47+
protected override void OnModelCreating(ModelBuilder builder)
48+
{
49+
base.OnModelCreating(builder);
50+
builder.Entity<LevelData>();
51+
}
52+
}
53+
```
54+
55+
## 2. Create `SQLiteContextFactory` class
56+
57+
```C#
58+
using Microsoft.EntityFrameworkCore;
59+
60+
public class SQLiteContextFactory : EFCoreSQLiteBundle.SQLiteContextFactory<SQLiteContext>
61+
{
62+
protected override string DesignTimeDataSource => "replace it with path to design time database";
63+
64+
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db") { }
65+
66+
protected override SQLiteContext InternalCreateDbContext(DbContextOptions<SQLiteContext> optionsBuilder)
67+
=> new SQLiteContext(optionsBuilder);
68+
}
69+
```
70+
71+
The `EFCoreSQLiteBundle.SQLiteContextFactory` class under the hood executes `SQLitePCLRaw.Startup.Setup();` for proper SQLite setup depends on the current platform.
72+
73+
## 3. Create database context
74+
75+
```C#
76+
var contextFactory = new SQLiteContextFactory();
77+
var dbContext = contextFactory.CreateDbContext();
78+
79+
// use it for data manipulations
80+
// sample:
81+
var level_1 = dbContext.Levels.FirstOrDefault(level => level.id == 1);
82+
```
83+
84+
# Helpful information
85+
86+
Read more how to use [EntityFrameworkCore](https://learn.microsoft.com/en-us/ef/ef6/get-started?redirectedfrom=MSDN). My favorite approach is `Code First`.
87+
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!
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "EFCore.SQLite.Bundle"
3+
}

Assets/_PackageRoot/Plugins/SQLitePCLRaw.provider.sqlite3.2.1.10.meta renamed to Assets/_PackageRoot/Runtime/EFCore.SQLite.Bundle.asmdef.meta

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_PackageRoot/Runtime/SQLiteContextFactory.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,55 @@
22
using Microsoft.EntityFrameworkCore;
33
using Microsoft.EntityFrameworkCore.Design;
44

5-
namespace Microsoft.EntityFrameworkCore
5+
namespace EFCoreSQLiteBundle
66
{
77
public abstract class SQLiteContextFactory<T> : IDbContextFactory<T>, IDesignTimeDbContextFactory<T> where T : DbContext
88
{
9-
protected abstract string FolderPath_runtime { get; }
10-
protected abstract string FolderPath_designTime { get; }
11-
protected virtual string DatabasePath_designTime => $"Data Source={FolderPath_designTime}/data.db";
12-
protected virtual string DatabasePath_runtime => $"Data Source={FolderPath_runtime}/data.db";
9+
/// <summary>
10+
/// The "design time" is only needed in the case if you would like to use the EF Core tools
11+
/// For example, to create a migration or update the database schema.
12+
/// If you don't need to use the EF Core tools, you can return null here.
13+
///
14+
/// Provide the connection string for design time usage.
15+
/// It should be something like "Data Source=database.db"
16+
/// </summary>
17+
protected abstract string DesignTimeDataSource { get; }
18+
protected virtual string DataSource => $"Data Source={_dataSource}";
1319

20+
private readonly string _dataSource;
21+
private readonly string _directoryPath;
22+
23+
/// <summary>
24+
/// Default constructor needed for Design Time.
25+
/// Don't use it in runtime.
26+
/// </summary>
1427
public SQLiteContextFactory()
1528
{
1629
SQLitePCLRaw.Startup.Setup();
1730
}
18-
public SQLiteContextFactory(string folderPath) : this()
31+
public SQLiteContextFactory(string directoryPath, string databaseFilename)
1932
{
20-
EnsureDatabaseFolderExists(folderPath);
33+
_directoryPath = directoryPath;
34+
_dataSource = Path.Combine(directoryPath, databaseFilename);
35+
EnsureDatabaseFolderExists();
36+
37+
SQLitePCLRaw.Startup.Setup();
2138
}
22-
private void EnsureDatabaseFolderExists(string folderPath)
39+
private void EnsureDatabaseFolderExists()
2340
{
24-
if (!Directory.Exists(folderPath))
25-
Directory.CreateDirectory(folderPath);
41+
if (!Directory.Exists(_directoryPath))
42+
Directory.CreateDirectory(_directoryPath);
2643
}
2744

2845
protected abstract T InternalCreateDbContext(DbContextOptions<T> optionsBuilder);
2946

3047
// Runtime usage
3148
public T CreateDbContext()
3249
{
33-
EnsureDatabaseFolderExists(FolderPath_runtime);
50+
EnsureDatabaseFolderExists();
3451

3552
var optionsBuilder = new DbContextOptionsBuilder<T>()
36-
.UseSqlite(DatabasePath_runtime);
53+
.UseSqlite(DataSource);
3754

3855
var dbContext = InternalCreateDbContext(optionsBuilder.Options);
3956

@@ -45,10 +62,8 @@ public T CreateDbContext()
4562
// Only design time usage
4663
T IDesignTimeDbContextFactory<T>.CreateDbContext(string[] args)
4764
{
48-
EnsureDatabaseFolderExists(FolderPath_designTime);
49-
5065
var optionsBuilder = new DbContextOptionsBuilder<T>()
51-
.UseSqlite(DatabasePath_designTime);
66+
.UseSqlite(DesignTimeDataSource);
5267

5368
return InternalCreateDbContext(optionsBuilder.Options);
5469
}

Assets/_PackageRoot/Runtime/SQLiteContextFactory.cs.meta

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)