MasLazu.AspNet.Authorization.Core.EfCore 1.0.0-preview.7

This is a prerelease version of MasLazu.AspNet.Authorization.Core.EfCore.
dotnet add package MasLazu.AspNet.Authorization.Core.EfCore --version 1.0.0-preview.7
                    
NuGet\Install-Package MasLazu.AspNet.Authorization.Core.EfCore -Version 1.0.0-preview.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MasLazu.AspNet.Authorization.Core.EfCore" Version="1.0.0-preview.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MasLazu.AspNet.Authorization.Core.EfCore" Version="1.0.0-preview.7" />
                    
Directory.Packages.props
<PackageReference Include="MasLazu.AspNet.Authorization.Core.EfCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MasLazu.AspNet.Authorization.Core.EfCore --version 1.0.0-preview.7
                    
#r "nuget: MasLazu.AspNet.Authorization.Core.EfCore, 1.0.0-preview.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MasLazu.AspNet.Authorization.Core.EfCore@1.0.0-preview.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MasLazu.AspNet.Authorization.Core.EfCore&version=1.0.0-preview.7&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=MasLazu.AspNet.Authorization.Core.EfCore&version=1.0.0-preview.7&prerelease
                    
Install as a Cake Tool

MasLazu.AspNet.Authorization.Core.EfCore

This project provides the Entity Framework Core implementation for the MasLazu ASP.NET Authorization system. It configures the database context, entity mappings, relationships, and provides the data access layer for the authorization domain.

Overview

The EF Core layer serves as the data access component, translating domain entities into database tables and managing relationships between authorization entities. It uses Entity Framework Core to provide efficient querying, change tracking, and database operations.

Architecture

EF Core Layer
├── Data/              # Database context
├── Configurations/    # Entity type configurations
└── Extensions/        # Dependency injection setup

Database Context

AuthorizationCoreDbContext

The main database context inheriting from BaseDbContext:

public class AuthorizationCoreDbContext : BaseDbContext
{
    public AuthorizationCoreDbContext(DbContextOptions<AuthorizationCoreDbContext> options) : base(options)
    {
    }

    public DbSet<Permission> Permissions { get; set; }
    public DbSet<PermissionType> PermissionTypes { get; set; }
    public DbSet<Resource> Resources { get; set; }
    public DbSet<ResourceAction> ResourceActions { get; set; }
    public DbSet<Domain.Entities.Action> Actions { get; set; }
}

Entity Configurations

Each domain entity has a corresponding configuration class implementing IEntityTypeConfiguration<TEntity>:

ActionConfiguration

  • Primary key: Id
  • Required properties: Code (50 chars), Name (100 chars)
  • Unique index on Code

PermissionConfiguration

  • Primary key: Id
  • Required properties: Code (100 chars), Name (100 chars), Description (500 chars), TypeCode (10 chars)
  • Unique index on Code

PermissionTypeConfiguration

  • Primary key: Id
  • Required properties: Code (50 chars), Description (200 chars)
  • Unique index on Code

ResourceConfiguration

  • Primary key: Id
  • Required properties: Code (50 chars), Name (100 chars)
  • Unique index on Code

ResourceActionConfiguration

Complex configuration for the junction entity:

  • Primary key: Id
  • Required properties: PermissionCode, ResourceCode, ActionCode
  • Foreign key relationships:
    • Resource via ResourceCodeResource.Code
    • Action via ActionCodeAction.Code
    • Permission via PermissionCodePermission.Code
  • Cascade delete behavior
  • Unique index on PermissionCode
  • Composite index on (ResourceCode, ActionCode)

Database Schema

The configurations create the following table structure:

Actions
├── Id (PK)
├── Code (UQ, 50)
├── Name (100)
├── CreatedAt
└── UpdatedAt

Permissions
├── Id (PK)
├── Code (UQ, 100)
├── Name (100)
├── Description (500)
├── TypeCode (10)
├── CreatedAt
└── UpdatedAt

PermissionTypes
├── Id (PK)
├── Code (UQ, 50)
├── Description (200)
├── CreatedAt
└── UpdatedAt

Resources
├── Id (PK)
├── Code (UQ, 50)
├── Name (100)
├── CreatedAt
└── UpdatedAt

ResourceActions
├── Id (PK)
├── PermissionCode (FK → Permissions.Code, UQ)
├── ResourceCode (FK → Resources.Code)
├── ActionCode (FK → Actions.Code)
├── CreatedAt
├── UpdatedAt
└── IX: (ResourceCode, ActionCode)

Relationships

The entity configurations establish the following relationships:

  • ResourceAction → Permission: Many-to-one via PermissionCode
  • ResourceAction → Resource: Many-to-one via ResourceCode
  • ResourceAction → Action: Many-to-one via ActionCode

All relationships use cascade delete, meaning deleting a Permission/Resource/Action will remove associated ResourceActions.

Key Design Decisions

Code-Based Foreign Keys

Unlike typical EF Core setups using Id-based foreign keys, this implementation uses Code-based relationships:

  • Allows for more readable and meaningful foreign key values
  • Supports business logic based on codes rather than surrogate keys
  • Requires unique constraints on Code fields

Unique Constraints

  • All entity Codes have unique indexes
  • PermissionCode in ResourceActions is unique (one permission per resource-action combination)
  • Composite index on (ResourceCode, ActionCode) for efficient queries

Dependencies

This project depends on:

  • MasLazu.AspNet.Authorization.Core.Domain - Domain entities
  • Microsoft.EntityFrameworkCore - EF Core ORM
  • MasLazu.AspNet.Framework.EfCore - Base context and framework components

Usage

Database Context Registration

services.AddDbContext<AuthorizationCoreDbContext>(options =>
    options.UseSqlServer(connectionString));

Migration and Database Setup

// Generate migration
dotnet ef migrations add InitialCreate --project src/MasLazu.AspNet.Authorization.Core.EfCore

// Apply migration
dotnet ef database update --project src/MasLazu.AspNet.Authorization.Core.EfCore

Repository Pattern Integration

The DbContext integrates with the repository pattern through:

  • IRepository<TEntity> implementations
  • IUnitOfWork for transaction management
  • IReadRepository<TEntity> for read operations

Integration

This EF Core project integrates with:

  • Domain Layer: Maps domain entities to database tables
  • Core Layer: Provides data access for service implementations
  • Application Layer: Supports business operations with transactional data access

The EF Core layer ensures efficient data persistence while maintaining the domain model's integrity and relationships.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-preview.7 138 10/9/2025
1.0.0-preview.6 128 10/9/2025
1.0.0-preview.5 128 10/9/2025
1.0.0-preview.1 171 9/20/2025