Skip to content

Commit d5fa14b

Browse files
committed
Refactor README to enhance usage instructions and add detailed examples for database operations
1 parent 0047f0d commit d5fa14b

File tree

2 files changed

+105
-24
lines changed

2 files changed

+105
-24
lines changed

Assets/Sample/2. DatabaseOperations/Scripts/Database/SQLiteContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
1111
{
1212
base.OnModelCreating(modelBuilder);
1313
modelBuilder.Entity<LevelData>();
14+
15+
// To create connection between tables read more about Code First EFCore approach
16+
// Highly recommended to use command line to generate the code automatically.
1417
}
1518
}

README.md

Lines changed: 102 additions & 24 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
@@ -47,11 +39,32 @@ Then use EFCore as usual.
4739
openupm add extensions.unity.bundle.efcore.sqlite
4840
```
4941

50-
# Alternative usage
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`
49+
50+
```csharp
51+
using System.ComponentModel.DataAnnotations;
5152

52-
## 1. Create `SQLiteContext` class
53+
public class LevelData
54+
{
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; }
62+
}
63+
```
5364

54-
```C#
65+
### 2. Create `SQLiteContext.cs`
66+
67+
```csharp
5568
using Microsoft.EntityFrameworkCore;
5669

5770
public class SQLiteContext : DbContext
@@ -66,39 +79,104 @@ public class SQLiteContext : DbContext
6679
{
6780
base.OnModelCreating(builder);
6881
builder.Entity<LevelData>();
82+
83+
// To create connection between tables read more about Code First EFCore approach
84+
// Highly recommended to use command line to generate the code automatically.
6985
}
7086
}
7187
```
7288

73-
## 2. Create `SQLiteContextFactory` class
89+
### 3. Create `SQLiteContextFactory.cs`
7490

75-
```C#
91+
```csharp
7692
using Microsoft.EntityFrameworkCore;
7793

7894
public class SQLiteContextFactory : EFCoreSQLiteBundle.SQLiteContextFactory<SQLiteContext>
7995
{
80-
protected override string DesignTimeDataSource => "replace it with path to design time database";
81-
82-
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db") { }
96+
public SQLiteContextFactory() : base(UnityEngine.Application.persistentDataPath, "data.db")
97+
{
98+
// Optional logging
99+
UnityEngine.Debug.Log($"Using database: {DataSource}");
100+
}
83101

84102
protected override SQLiteContext InternalCreateDbContext(DbContextOptions<SQLiteContext> optionsBuilder)
85-
=> new SQLiteContext(optionsBuilder);
103+
{
104+
return new SQLiteContext(optionsBuilder);
105+
}
86106
}
87107
```
88108

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

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

93-
```C#
94-
var contextFactory = new SQLiteContextFactory();
95-
var dbContext = contextFactory.CreateDbContext();
166+
---
96167

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

178+
---
179+
102180
# Helpful information
103181

104182
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)