Database
At the heart of nearly every application is a database, the place where all your important information is stored, managed, and retrieved. Choosing the right database and making it work seamlessly with your application is a critical step, and SliceFlow is designed to make this process as smooth and powerful as possible.
SliceFlow’s weapon of choice is the combination of Entity Framework Core (EF Core) and PostgreSQL. EF Core is a modern tool from Microsoft that simplifies database interactions, letting you work with your data using familiar C# objects instead of writing raw SQL. PostgreSQL is a rock-solid, open-source database known for its reliability, performance, and advanced features. Together, they provide a robust foundation for your application’s data layer.
Getting Connected in Two Simple Steps
Connecting your SliceFlow application to your database is incredibly straightforward.
Step 1: Enable Database Services
First, you need to tell your application to use the database services. This is done with a single line in your Program.cs
file:
builder ... .ConfigureDatabase() ...
Step 2: Provide the Connection String
Next, your application needs to know where to find your database. You provide this information in a “connection string” in your appsettings.json
file. It’s like giving your app the address and login details for your database.
{ "ConnectionStrings": { "Default": "Server=localhost;Port=5432;User Id=postgres;Password=yourpassword;Database=yourdb;" }}
This string tells SliceFlow everything it needs to know: the server’s address (Server
), the port to communicate on (Port
), the username (User Id
) and Password
to log in with, and which Database
to use.
Powerful Features, Right Out of the Box
SliceFlow doesn’t just connect to a database; it enhances it with features designed to make your life easier and your application better.
- Flexible JSON Data: Sometimes your data doesn’t fit neatly into traditional rows and columns. SliceFlow embraces the flexibility of modern data by enabling PostgreSQL’s powerful JSON support. This means you can store and query complex, dynamic data structures right inside your database, giving you the best of both structured and unstructured worlds.
- Automatic Auditing: Remember that vigilant security camera we talked about in the Auditing documentation? This is where it gets plugged in. Every time you save changes to the database, SliceFlow’s auditing system automatically kicks in to record who changed what, and when.
- Smarter Debugging: When you’re hunting down a tricky bug, seeing the exact data being used in a query can be a lifesaver. SliceFlow understands this, so it automatically turns on detailed data logging when you’re in a development environment. This is a “development-only” superpower; in production, this feature is wisely disabled to protect your users’ sensitive information.
Keeping Your Database Up-to-Date, Effortlessly
As your application evolves, your database structure will need to change with it. Managing these changes can be tricky, but SliceFlow makes it a hands-off process.
When your application starts up, it automatically performs two key tasks:
- Applies Migrations: Think of migrations as version control for your database structure. SliceFlow checks if the database schema is in sync with your application’s code and, if not, automatically applies any pending changes. No more manual scripts or worrying if your database is out of date.
- Seeds Permissions: A secure application needs a solid foundation of permissions. Right after updating the database, SliceFlow ensures that all the necessary permissions are present and correct, keeping your application’s security rules in sync with your code. For more details, see the Permissions documentation.
This entire startup process is configured with just two lines in Program.cs
:
var app = builder.Build();
app.ApplyMigrations() .SeedPermissions();
With SliceFlow, the complex and often error-prone task of database configuration and maintenance is handled for you, so you can focus on what really matters: building amazing features for your users.