| Clean Architecture Toolkit for .NET | |||||
|
Clean Architecture Toolkit is a clean, modular architecture foundation for .NET applications. It embraces SOLID principles and focuses on maintainability, testability, and scalability through clear boundaries and pluggable design. |
|
||||
|
| HOME | MODULE OVERVIEW | DEMO | |
|||||
Cat.EFCore
Set up Entity Framework Core entities the same way every time.
Inherit from a base class, call one extension method in OnModelCreating, and get a Guid key, an identity sequence column, and optional temporal tables.
Table of contents
NuGet packages
| Package | Role | Install | Downloads |
|---|---|---|---|
| Cat.EFCore.Entities | Entity base classes | dotnet add package gmarginet.Cat.EFCore.Entities |
|
| Cat.EFCore.Extensions | Model builder extensions | dotnet add package gmarginet.Cat.EFCore.Extensions |
Namespaces
| Namespace | What it holds |
|---|---|
| Cat.EFCore.Entities | Base classes for regular, temporal and reference data entities. |
| Cat.EFCore.Extensions | Model builder extensions that configure keys, indexes and tables for the base classes. |
Dependencies
Cat.EFCore has no dependencies on other CAT modules.
Usage
Inherit your entities from the Cat base classes.
public class Todo : CatEntityBase
{
public string Title { get; set; } = string.Empty;
public Guid TodoOwnerId { get; set; }
public TodoOwner? TodoOwner { get; set; }
}
public class TodoHistoryItem : CatTemporalEntityBase
{
public string Title { get; set; } = string.Empty;
}
// The table belongs to another service that shares the database.
public class TodoOwner : CatReferenceEntityBase
{
public string Name { get; set; } = string.Empty;
}
Configure them in OnModelCreating.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ConfigureCatEntity<Todo>("Todo", "todo");
modelBuilder.ConfigureCatTemporalEntity<TodoHistoryItem>("TodoHistoryItem", "todo");
modelBuilder.ConfigureCatReferenceEntity<TodoOwner>("TodoOwner", "identity");
modelBuilder.Entity<Todo>()
.HasOne(t => t.TodoOwner)
.WithMany()
.HasForeignKey(t => t.TodoOwnerId);
}
What you get:
CatEntityBase: a non-clustered Guid primary key onIdand a unique clustered index on the identity columnSequenceId.CatTemporalEntityBase: the same as above, in a temporal table with a history table in theHistoryschema.CatReferenceEntityBase: a non-clustered primary key, and the table is excluded from migrations.
When to use CatReferenceEntityBase
Use it for a table that someone else owns, so you can point a foreign key at it.
- Several services share one database. Each service owns some of the tables.
- Your service needs a foreign key or navigation to a table of another service.
- You map that table in your own
DbContextwithCatReferenceEntityBase. Migrations never touch it, so the owner keeps control of the schema. - Treat the entity as read-only. Only the owner changes the data.
Do not use it for your own tables. Use CatEntityBase for those, even when the data is a short list of fixed values.