SQLiteORM.Net is a Code First SQLite ORM written for .NET for use in Unity projects. It is inspired by and attempting to emulate both the syntaxes and architectures of EntityFramework, but for SQLite.
Download the SQLiteORM.Net.Core.unitypackage
and import it into your project.
You will also need to download the two dependencies, I unfortunately could not package the SQLite Database code since it's a paid asset available on the Unity Asset Store.
I chose SQLite because it's extremely lightweight and most importantly it supports iOS, Android, PC and Mac.
JsonDotNet is free and also available on the Unity Asset Store.
The SQLiteORM.Net.unitypackage
includes the JsonDotNet binaries.
- SQLiteDatabase - $10
- JsonDotNet - FREE
- Error events
- Foreign keys and Navigations Properties
- Migrations
- Change tracking and SaveChanges()/Rollbacks
Databases are represented by the SQLiteDbContext
{:.language-cs} abstract class.
To create your own database simply create a class inheriting from that base class,
public class ExampleDbContext : SQLiteDBContext {}
Then, in your code, simply create a new instance of your SQLiteDBContext
.
using (var dbContext = new ExampleDbContext())
{
// Db Code Here using the dbContext variable
}
As a code-first solution, your tables are represented by models. Simply create a class for the data that you want to store in your database,
public class User
{
[PrimaryKey]
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
By default, the name of the class will be the name of the resulting table. Additionally, the names of all the properties will be the names of all the columsn in that table.
Tables are represented by the SQLiteTable<>
{:.language-cs} generic class.
Simply add a public SQLiteTable
{:.language-cs} property whose generic argument is the type
of the model you want to create a table for,
public class ExampleDbContext : SQLiteDBContext
{
public SQLiteTable<User> Users;
}
using (var dbContext = new ExampleDbContext())
{
string id = Guid.NewGuid().ToString();
// Insert new data
bool successful = dbContext.Users.Insert(new User
{
Id = id,
Age = 26,
FirstName = "Christopher",
LastName = "Zumberge"
});
// Retrieve data
IEnumerable<User> users = dbContext.Users.GetItems();
// Update data
// Delete data
}
Coming soon....
For most all configuration changes to the database and dbcontext, you need to create a subclass
of the abstract SQLiteConfiguration
class.
This will allow you to change all the parameters that the DbContext uses at runtime.
public class ExampleSQLiteConfiguration : SQLiteConfiguration
{
public override string DatabaseLocation { get { return Application.persistentDataPath; } }
public override LoggingLevel LoggingLevel { get { return LoggingLevel.TRACE; } }
public override ISQLiteLogger SQLiteLogger { get { return new UnityConsoleSQLiteLogger(); } }
}
If you only wish to change a few of the parameters, inherit the DefaultSQLiteConfiguration
class instead, since all of the abstract fields are already given default values.
public class ExampleSQLiteConfiguration : DefaultSQLiteConfiguration
{
public override LoggingLevel LoggingLevel { get { return LoggingLevel.TRACE; } }
}
To specifiy the name of your database, simply pass a database name string to the base
constructor of your SQLiteDBContext
class,
public class ExampleDbContext : SQLiteDBContext {
public ExampleDbContext() : base("ExampleDatabase.db"){ }
}
To create a table for a model that has a different name than the class, use the [TableName]
attribute,
[TableName("User")]
public class TestUser
{
// ...
// ...
}
This will create a table named "User" instead of "TestUser", as it would do by default.
To create a column in a table that has a different name than the property it maps to, use the [FieldName]
attribute,
public class User
{
// ...
[FieldName("UserAge")]
public int Age { get; set; }
// ...
}
To not map a property to a table column, use the [ColumnIgnore]
attribute,
public class User
{
// ...
public string FirstName { get; set; }
public string LastName { get; set; }
// ...
[ColumnIgnore]
public string Name => $"{FirstName} {LastName}"
}
All of the above have attributes that you can decorate the intended properties with
public class User
{
[PrimaryKey]
public string Id { get; set; }
[Unique]
public sting SSN { get; set; }
[NotNull]
public string FirstName { get; set; }
[NotNull]
public string LastName { get; set; }
}
There is a Size Attribute that allows you to specify the size of a field that will be mapped back to varchar,
public class User
{
// ...
[Size(16)]
[Unique]
public string IdentificationNumber { get; set; }
// ...
}
Not yet supported...
Since the ORM is Code-First, provide a default value just by setting the class property equal to a value.
public class User
{
// ...
public string FirstName { get; set; } = String.Empty;
// ...
}