Cloudey.Reflex.Database
1.0.5
Prefix Reserved
See the version list below for details.
dotnet add package Cloudey.Reflex.Database --version 1.0.5
NuGet\Install-Package Cloudey.Reflex.Database -Version 1.0.5
<PackageReference Include="Cloudey.Reflex.Database" Version="1.0.5" />
paket add Cloudey.Reflex.Database --version 1.0.5
#r "nuget: Cloudey.Reflex.Database, 1.0.5"
// Install Cloudey.Reflex.Database as a Cake Addin #addin nuget:?package=Cloudey.Reflex.Database&version=1.0.5 // Install Cloudey.Reflex.Database as a Cake Tool #tool nuget:?package=Cloudey.Reflex.Database&version=1.0.5
Reflex
Database
Opinionated Entity Framework Core setup for the Reflex framework, using PostgreSQL as the database.
- Auto-discovery of entity configurations in included assemblies (using the Reflex.Core
IncludeAssembly
attribute) - Automatic Created and Updated fields for entities
- Integration with ASP.NET Core Identity
- Ulid keys used for entities, incl. support for storing Ulid as Guid in the database
Installation
Install with NuGet
Setup
To get started, create a database context inheriting from the ReflexDatabaseContext
class, passing in your Identity entities:
public class MainDb : ReflexDatabaseContext<User, Role, MainDb>
{
public MainDb (DbContextOptions<MainDb> options) : base(options) { }
}
Add database configuration to your settings file (see the configuration section in Cloudey.Reflex.Core):
Database:
MainDb: # Must match the database context class name
Host: 127.0.0.1
User: MyApp
Password: MyApp
Port: 5432
Database: MyApp
Register the database context with DI:
// Program.cs
builder.Services.AddDatabase<MainDb>(builder.Configuration)
Also register the database module with Autofac to enable auto-discovery of triggers:
public class MyModule : Module
{
protected override void Load (ContainerBuilder builder)
{
// ...
builder.RegisterModule<DatabaseModule>();
}
}
Entities in your application must implement the IEntity
interface. You can optionally create an abstract Entity
type to avoid re-defining the properties in every entity:
public abstract class Entity : IEntity
{
public Ulid Id { get; set; } // Stored as a Guid in the database and automatically converted
public Guid Guid // The Guid field is not stored in the database and is provided for convenience
{
get => Id.ToGuid();
set => Id = new Ulid(value);
}
public DateTime Created { get; set; } = DateTime.MinValue; // Default value for easier seeding
public DateTime Updated { get; set; } = DateTime.MinValue;
public Ulid Revision { get; set; }
}
If using GraphQL (see Cloudey.Reflex.GraphQL), register the database context with the GraphQL builder:
var builder = services.AddGraphQLServer()
// ...
builder.RegisterDbContext<MainDb>(DbContextKind.Pooled);
Entities
You can configure entities in your application using the EntityConfiguration<TEntity>
class:
public class ToDoItem : Entity // Using the abstract base class, you can also use IEntity if you like
{
public string Title { get; set; }
public bool IsComplete { get; set; }
public class Configuration : EntityConfiguration<Hello> {} // Configure this type as a database entity (required)
}
Configuring entities and seeding
You can configure the entities in the Configuration class for each entity:
public class ToDoItem : Entity // Using the abstract base class, you can also use IEntity if you like
{
public string Title { get; set; }
// ...
public class Configuration : EntityConfiguration<Hello> {
public override void Configure(EntityTypeBuilder<Hello> builder)
{
// IMPORTANT: Always call base.Configure(builder) first to make the ID, revisions and timestamps are automatically generated
base.Configure(builder);
// Add your own configuration
builder.HasIndex(x => x.Title).IsUnique();
// Seed some data
builder.HasData(
new ToDoItem
{
// ID and revision must be set manually on seeding (EF Core limitation)
Id = new Ulid(new Guid("68bbcab1-4aec-4117-8ed9-1101f4768825")),
Revision = new Ulid(new Guid("35f9f62b-1d5e-4a37-87b9-b3372771425e")),
Title = "My seeded to-do item",
},
)
}
}
}
Made by
License
Licensed under Apache 2.0.
Copyright © 2023 Cloudey IT Ltd
Cloudey® is a registered trademark of Cloudey IT Ltd. Use of the trademark is NOT GRANTED under the license of this repository or software package.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Autofac (>= 7.0.1)
- Cloudey.Reflex.Core (>= 1.0.2)
- EntityFrameworkCore.Triggers (>= 1.2.3)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 7.0.5)
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.5)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 7.0.4)
- Ulid (>= 1.2.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.