This section shows how to setup and configure your database connection in your Piranha application and is intended for users who wants to create their application from scratch and not have this code generated by any of the available project templates.
More information on
Entity Framework Core
can be found here.
Even though Piranha can be used with any kind of data mapper or data layer we only provide packages for Entity Framework Core
.
Entity Framework is a modern object-database mapper for .NET provided by Microsoft. It supports LINQ
queries, change tracking
, updates
, and schema migrations
. EF Core works with many databases, including SQL Database
(on-premises and Azure), SQLite
, MySQL
, PostgreSQL
, and Azure Cosmos DB
.
In Entity Framework schema migrations are database dependent, this means that you need different packages depending on which database you are targeting. It also means that you can't just copy a database schema from one type of database to another. With this said it's always easier to use the same type of database for development as you will use in production.
These are the different databases that we currently support.
Database | Context Name | NuGet Package |
---|---|---|
MySql | MySqlDb |
Piranha.Data.EF.MySql |
PostgreSql | PostgreSqlDb |
Piranha.Data.EF.PostgreSql |
SQL Server | SQLServerDb |
Piranha.Data.EF.SQLServer |
SQLite | SQLiteDb |
Piranha.Data.EF.SQLite |
Here we will take a look at the basics for configuring Piranha for Entity Framework Core. Specifics on the different database packages can be found in the Databases article in the Architecture section.
The current Entity Framework context that should be available should be configured in the method ConfigureServices
in Startup.cs
. In this example we're using the DbContext
for SQLite
as shown in the table above, but this should be changed to the database you're currently using.
public void ConfigureServices(IServiceCollection services)
{
services.AddPiranha(options =>
{
...
options.UseEF<SQLiteDb>(db =>
db.UseSqlite("Filename=./PiranhaWeb.db"));
});
}
For more information, please refer to the Databases article in the Architecture section.