agentskills.codes
EF

efcore-patterns

Entity Framework Core patterns for ASP.NET Zero (DbContext, repositories, migrations)

Install

mkdir -p .claude/skills/efcore-patterns && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/15654" && unzip -o skill.zip -d .claude/skills/efcore-patterns && rm skill.zip

Installs to .claude/skills/efcore-patterns

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Entity Framework Core patterns for ASP.NET Zero (DbContext, repositories, migrations)
85 charsno explicit “when” trigger

About this skill

EF Core Patterns for ASP.NET Zero

Quick Start

After creating an entity in Core, register it in the EntityFrameworkCore project:

1. Add DbSet to DbContext

// aspnet-core/src/CadentManagement.EntityFrameworkCore/EntityFrameworkCore/CadentManagementDbContext.cs
// Add property inside the class:

public DbSet<Product> Products { get; set; }

2. Configure Entity (if needed)

// In OnModelCreating method of CadentManagementDbContext:

modelBuilder.Entity<Product>(b =>
{
    b.ToTable("Products");
    b.HasIndex(e => e.TenantId);
    b.Property(e => e.Name).HasMaxLength(256).IsRequired();
    b.Property(e => e.Price).HasPrecision(18, 2);
});

3. Create Migration

# From the EntityFrameworkCore project directory
cd aspnet-core/src/CadentManagement.EntityFrameworkCore
dotnet ef migrations add Added_Product_Entity

DbContext Structure

aspnet-core/src/CadentManagement.EntityFrameworkCore/
  EntityFrameworkCore/
    CadentManagementDbContext.cs              # Main DbContext
    CadentManagementEntityFrameworkCoreModule.cs
    Repositories/
      CadentManagementRepositoryBase.cs      # Custom repository base

The DbContext inherits from AbpZeroDbContext<Tenant, Role, User, CadentManagementDbContext>.

Custom Repository (When Needed)

Only create custom repositories when IRepository<T> doesn't suffice (complex queries):

// Interface in Core project
namespace CadentManagement.{Feature};

public interface IProductRepository : IRepository<Product>
{
    Task<List<Product>> GetExpensiveProductsAsync(decimal minPrice);
}

// Implementation in EntityFrameworkCore project
namespace CadentManagement.EntityFrameworkCore.Repositories;

public class ProductRepository : CadentManagementRepositoryBase<Product>, IProductRepository
{
    public ProductRepository(IDbContextProvider<CadentManagementDbContext> dbContextProvider)
        : base(dbContextProvider) { }

    public async Task<List<Product>> GetExpensiveProductsAsync(decimal minPrice)
    {
        return await GetAll()
            .Where(p => p.Price >= minPrice)
            .OrderByDescending(p => p.Price)
            .ToListAsync();
    }
}

Fluent API Common Configurations

ConfigurationCode
Table nameb.ToTable("Products")
Required stringb.Property(e => e.Name).HasMaxLength(256).IsRequired()
Decimal precisionb.Property(e => e.Price).HasPrecision(18, 2)
Indexb.HasIndex(e => e.TenantId)
Composite indexb.HasIndex(e => new { e.TenantId, e.Name })
Unique indexb.HasIndex(e => e.Code).IsUnique()
Default valueb.Property(e => e.IsActive).HasDefaultValue(true)
One-to-manyb.HasMany(e => e.Items).WithOne(i => i.Order).HasForeignKey(i => i.OrderId)

Migration Commands

All commands must be run from the EntityFrameworkCore project directory:

cd aspnet-core/src/CadentManagement.EntityFrameworkCore

# 1. Build first (always required before migrations)
dotnet build

# 2. Create migration
dotnet ef migrations add {MigrationName}

# 3. Apply migration to database
dotnet ef database update

# Remove last migration (only if NOT yet applied)
dotnet ef migrations remove

ASP.NET Zero: Applying Migrations

ASP.NET Zero has a dedicated Migrator console project at: aspnet-core/src/CadentManagement.Migrator/

In production or staging, run the Migrator to apply pending migrations:

dotnet run --project aspnet-core/src/CadentManagement.Migrator

In development, dotnet ef database update from the EntityFrameworkCore directory is sufficient.

Constraints

  • Never put business logic in repositories
  • Custom repos inherit from CadentManagementRepositoryBase<TEntity>
  • Never access DbContext from Application layer
  • Use Fluent API for configuration, not data annotations on entities

Reference Files

  • DbContext: aspnet-core/src/CadentManagement.EntityFrameworkCore/EntityFrameworkCore/CadentManagementDbContext.cs
  • Repository base: aspnet-core/src/CadentManagement.EntityFrameworkCore/EntityFrameworkCore/Repositories/CadentManagementRepositoryBase.cs
  • Custom repository: aspnet-core/src/CadentManagement.EntityFrameworkCore/EntityFrameworkCore/Authorization/Users/UserRepository.cs

Checklist

  • DbSet<TEntity> added to CadentManagementDbContext
  • Entity configured in OnModelCreating (if needed)
  • Migration created with dotnet ef migrations add (from EntityFrameworkCore directory)
  • Migration applied with dotnet ef database update (from EntityFrameworkCore directory)
  • Build succeeds after migration
  • Custom repository only if IRepository<T> is insufficient

Search skills

Search the agent skills registry