Skip to content

Commit bf03509

Browse files
committed
Refactor README.md to enhance usage instructions and examples for EFCore with SQLite
1 parent 07456d8 commit bf03509

File tree

2 files changed

+204
-90
lines changed

2 files changed

+204
-90
lines changed

Assets/_PackageRoot/Documentation~/README.md

Lines changed: 102 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ Supports AOT an JIT compilation. For AOT it uses nested `link.xml` file to exclu
2727
- ✔️ `.NET Standard 2.0`
2828
- ✔️ `.NET Standard 2.1`
2929

30-
# Usage
31-
32-
Call the function once at app startup. Important to do that before opening SQLite connection.
33-
34-
```C#
35-
SQLitePCLRaw.Startup.Setup();
36-
```
37-
3830
Then use EFCore as usual.
3931

4032
# Installation
@@ -44,38 +36,35 @@ Then use EFCore as usual.
4436
- Run the command
4537

4638
``` CLI
47-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
48-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
49-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
50-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
51-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
52-
39+
openupm add extensions.unity.bundle.efcore.sqlite
5340
```
5441

55-
- Modify `/Packages/manifest.json` file by adding required `dependencies` and `scopedRegistries`
42+
# Usage
43+
44+
## Option 1: Explicit (Recommended)
45+
46+
Use this approach to setup your database and establish connection.
47+
48+
### 1. Create data model `LevelData.cs`
5649

57-
```json
50+
```csharp
51+
using System.ComponentModel.DataAnnotations;
52+
53+
public class LevelData
5854
{
59-
"dependencies": {
60-
"extensions.unity.bundle.efcore.sqlite": "0.0.7"
61-
},
62-
"scopedRegistries": [
63-
{
64-
"name": "package.openupm.com",
65-
"url": "https://package.openupm.com",
66-
"scopes": [
67-
"com.openupm"
68-
]
69-
}
70-
]
55+
[Key]
56+
public int Id { get; set; }
57+
[StringLength(100)]
58+
public string Name { get; set; }
59+
[Range(1, 10)]
60+
public int Difficulty { get; set; }
61+
public string Description { get; set; }
7162
}
7263
```
7364

74-
# Alternative usage
65+
### 2. Create `SQLiteContext.cs`
7566

76-
## 1. Create `SQLiteContext` class
77-
78-
```C#
67+
```csharp
7968
using Microsoft.EntityFrameworkCore;
8069

8170
public class SQLiteContext : DbContext
@@ -90,39 +79,107 @@ public class SQLiteContext : DbContext
9079
{
9180
base.OnModelCreating(builder);
9281
builder.Entity<LevelData>();
82+
83+
// To define relationships between tables, configure navigation properties and use Fluent API.
84+
// Refer to the official EF Core documentation: https://learn.microsoft.com/en-us/ef/core/modeling/relationships
85+
// To generate code automatically, use the EF Core CLI tools. For example:
86+
// dotnet ef migrations add <MigrationName>
87+
// dotnet ef database update
9388
}
9489
}
9590
```
9691

97-
## 2. Create `SQLiteContextFactory` class
92+
### 3. Create `SQLiteContextFactory.cs`
9893

99-
```C#
94+
```csharp
10095
using Microsoft.EntityFrameworkCore;
10196

10297
public class SQLiteContextFactory : EFCoreSQLiteBundle.SQLiteContextFactory<SQLiteContext>
10398
{
104-
protected override string DesignTimeDataSource => "replace it with path to design time database";
105-
106-
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db") { }
99+
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db")
100+
{
101+
// Optional logging
102+
UnityEngine.Debug.Log($"Using database: {DataSource}");
103+
}
107104

108105
protected override SQLiteContext InternalCreateDbContext(DbContextOptions<SQLiteContext> optionsBuilder)
109-
=> new SQLiteContext(optionsBuilder);
106+
{
107+
return new SQLiteContext(optionsBuilder);
108+
}
110109
}
111110
```
112111

113112
The `EFCoreSQLiteBundle.SQLiteContextFactory` class under the hood executes `SQLitePCLRaw.Startup.Setup();` for proper SQLite setup depends on the current platform.
114113

115-
## 3. Create database context
114+
### 4. Create database context
115+
116+
```csharp
117+
using (var dbContext = new SQLiteContextFactory().CreateDbContext())
118+
{
119+
// use it for data manipulations
120+
// sample:
121+
var level_1 = dbContext.Levels.FirstOrDefault(level => level.Id == 1);
122+
}
123+
```
124+
125+
There is full usage sample in this source code:
126+
127+
```csharp
128+
using System.Linq;
129+
using UnityEngine;
130+
131+
public class DatabaseOperations : MonoBehaviour
132+
{
133+
void Awake()
134+
{
135+
AddLevel("Level 1", 1, "Easy level");
136+
AddLevel("Level 2", 2, "Medium level");
137+
AddLevel("Level 3", 3, "Hard level");
138+
139+
PrintAllLevels();
140+
}
141+
142+
void AddLevel(string name, int difficulty, string description)
143+
{
144+
using (var dbContext = new SQLiteContextFactory().CreateDbContext())
145+
{
146+
var level = new LevelData
147+
{
148+
Name = name,
149+
Difficulty = difficulty,
150+
Description = description
151+
};
152+
dbContext.Levels.Add(level);
153+
dbContext.SaveChanges();
154+
}
155+
Debug.Log($"Added Level: {name}, Difficulty: {difficulty}");
156+
}
157+
void PrintAllLevels()
158+
{
159+
using (var dbContext = new SQLiteContextFactory().CreateDbContext())
160+
{
161+
var levels = dbContext.Levels.ToList();
162+
foreach (var level in levels)
163+
Debug.Log($"Level ID: {level.Id}, Name: {level.Name}, Difficulty: {level.Difficulty}");
164+
}
165+
}
166+
}
167+
```
116168

117-
```C#
118-
var contextFactory = new SQLiteContextFactory();
119-
var dbContext = contextFactory.CreateDbContext();
169+
---
120170

121-
// use it for data manipulations
122-
// sample:
123-
var level_1 = dbContext.Levels.FirstOrDefault(level => level.id == 1);
171+
## Option 2: Minimalistic
172+
173+
Use this option if you well understand how to operate with EFCore on your own.
174+
175+
Call the function once at app startup. Important to do that before opening SQLite connection. This method call prepares SQLite.
176+
177+
```csharp
178+
SQLitePCLRaw.Startup.Setup();
124179
```
125180

181+
---
182+
126183
# Helpful information
127184

128185
Read more how to use [EntityFrameworkCore](https://learn.microsoft.com/en-us/ef/ef6/get-started?redirectedfrom=MSDN). My favorite approach is `Code First`.

Assets/_PackageRoot/README.md

Lines changed: 102 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ Supports AOT an JIT compilation. For AOT it uses nested `link.xml` file to exclu
2727
- ✔️ `.NET Standard 2.0`
2828
- ✔️ `.NET Standard 2.1`
2929

30-
# Usage
31-
32-
Call the function once at app startup. Important to do that before opening SQLite connection.
33-
34-
```C#
35-
SQLitePCLRaw.Startup.Setup();
36-
```
37-
3830
Then use EFCore as usual.
3931

4032
# Installation
@@ -44,38 +36,35 @@ Then use EFCore as usual.
4436
- Run the command
4537

4638
``` CLI
47-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
48-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
49-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
50-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
51-
openupm add [email protected] --registry https://unitynuget-registry.openupm.com/
52-
39+
openupm add extensions.unity.bundle.efcore.sqlite
5340
```
5441

55-
- Modify `/Packages/manifest.json` file by adding required `dependencies` and `scopedRegistries`
42+
# Usage
43+
44+
## Option 1: Explicit (Recommended)
45+
46+
Use this approach to setup your database and establish connection.
47+
48+
### 1. Create data model `LevelData.cs`
5649

57-
```json
50+
```csharp
51+
using System.ComponentModel.DataAnnotations;
52+
53+
public class LevelData
5854
{
59-
"dependencies": {
60-
"extensions.unity.bundle.efcore.sqlite": "0.0.7"
61-
},
62-
"scopedRegistries": [
63-
{
64-
"name": "package.openupm.com",
65-
"url": "https://package.openupm.com",
66-
"scopes": [
67-
"com.openupm"
68-
]
69-
}
70-
]
55+
[Key]
56+
public int Id { get; set; }
57+
[StringLength(100)]
58+
public string Name { get; set; }
59+
[Range(1, 10)]
60+
public int Difficulty { get; set; }
61+
public string Description { get; set; }
7162
}
7263
```
7364

74-
# Alternative usage
65+
### 2. Create `SQLiteContext.cs`
7566

76-
## 1. Create `SQLiteContext` class
77-
78-
```C#
67+
```csharp
7968
using Microsoft.EntityFrameworkCore;
8069

8170
public class SQLiteContext : DbContext
@@ -90,39 +79,107 @@ public class SQLiteContext : DbContext
9079
{
9180
base.OnModelCreating(builder);
9281
builder.Entity<LevelData>();
82+
83+
// To define relationships between tables, configure navigation properties and use Fluent API.
84+
// Refer to the official EF Core documentation: https://learn.microsoft.com/en-us/ef/core/modeling/relationships
85+
// To generate code automatically, use the EF Core CLI tools. For example:
86+
// dotnet ef migrations add <MigrationName>
87+
// dotnet ef database update
9388
}
9489
}
9590
```
9691

97-
## 2. Create `SQLiteContextFactory` class
92+
### 3. Create `SQLiteContextFactory.cs`
9893

99-
```C#
94+
```csharp
10095
using Microsoft.EntityFrameworkCore;
10196

10297
public class SQLiteContextFactory : EFCoreSQLiteBundle.SQLiteContextFactory<SQLiteContext>
10398
{
104-
protected override string DesignTimeDataSource => "replace it with path to design time database";
105-
106-
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db") { }
99+
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db")
100+
{
101+
// Optional logging
102+
UnityEngine.Debug.Log($"Using database: {DataSource}");
103+
}
107104

108105
protected override SQLiteContext InternalCreateDbContext(DbContextOptions<SQLiteContext> optionsBuilder)
109-
=> new SQLiteContext(optionsBuilder);
106+
{
107+
return new SQLiteContext(optionsBuilder);
108+
}
110109
}
111110
```
112111

113112
The `EFCoreSQLiteBundle.SQLiteContextFactory` class under the hood executes `SQLitePCLRaw.Startup.Setup();` for proper SQLite setup depends on the current platform.
114113

115-
## 3. Create database context
114+
### 4. Create database context
115+
116+
```csharp
117+
using (var dbContext = new SQLiteContextFactory().CreateDbContext())
118+
{
119+
// use it for data manipulations
120+
// sample:
121+
var level_1 = dbContext.Levels.FirstOrDefault(level => level.Id == 1);
122+
}
123+
```
124+
125+
There is full usage sample in this source code:
126+
127+
```csharp
128+
using System.Linq;
129+
using UnityEngine;
130+
131+
public class DatabaseOperations : MonoBehaviour
132+
{
133+
void Awake()
134+
{
135+
AddLevel("Level 1", 1, "Easy level");
136+
AddLevel("Level 2", 2, "Medium level");
137+
AddLevel("Level 3", 3, "Hard level");
138+
139+
PrintAllLevels();
140+
}
141+
142+
void AddLevel(string name, int difficulty, string description)
143+
{
144+
using (var dbContext = new SQLiteContextFactory().CreateDbContext())
145+
{
146+
var level = new LevelData
147+
{
148+
Name = name,
149+
Difficulty = difficulty,
150+
Description = description
151+
};
152+
dbContext.Levels.Add(level);
153+
dbContext.SaveChanges();
154+
}
155+
Debug.Log($"Added Level: {name}, Difficulty: {difficulty}");
156+
}
157+
void PrintAllLevels()
158+
{
159+
using (var dbContext = new SQLiteContextFactory().CreateDbContext())
160+
{
161+
var levels = dbContext.Levels.ToList();
162+
foreach (var level in levels)
163+
Debug.Log($"Level ID: {level.Id}, Name: {level.Name}, Difficulty: {level.Difficulty}");
164+
}
165+
}
166+
}
167+
```
116168

117-
```C#
118-
var contextFactory = new SQLiteContextFactory();
119-
var dbContext = contextFactory.CreateDbContext();
169+
---
120170

121-
// use it for data manipulations
122-
// sample:
123-
var level_1 = dbContext.Levels.FirstOrDefault(level => level.id == 1);
171+
## Option 2: Minimalistic
172+
173+
Use this option if you well understand how to operate with EFCore on your own.
174+
175+
Call the function once at app startup. Important to do that before opening SQLite connection. This method call prepares SQLite.
176+
177+
```csharp
178+
SQLitePCLRaw.Startup.Setup();
124179
```
125180

181+
---
182+
126183
# Helpful information
127184

128185
Read more how to use [EntityFrameworkCore](https://learn.microsoft.com/en-us/ef/ef6/get-started?redirectedfrom=MSDN). My favorite approach is `Code First`.

0 commit comments

Comments
 (0)