EntegratorPro.EntityFrameworkCore 1.0.3

dotnet add package EntegratorPro.EntityFrameworkCore --version 1.0.3                
NuGet\Install-Package EntegratorPro.EntityFrameworkCore -Version 1.0.3                
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="EntegratorPro.EntityFrameworkCore" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntegratorPro.EntityFrameworkCore --version 1.0.3                
#r "nuget: EntegratorPro.EntityFrameworkCore, 1.0.3"                
#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.
// Install EntegratorPro.EntityFrameworkCore as a Cake Addin
#addin nuget:?package=EntegratorPro.EntityFrameworkCore&version=1.0.3

// Install EntegratorPro.EntityFrameworkCore as a Cake Tool
#tool nuget:?package=EntegratorPro.EntityFrameworkCore&version=1.0.3                

EfRepository.cs

Overview

EfRepository is a generic Entity Framework Core repository implementation that simplifies data access. It is designed to work with DbContext and provides base methods for common database operations, such as retrieving, inserting, updating, and deleting entities.

This implementation adheres to the guideline of making repositories reusable by applying generic type constraints. All operations are asynchronous and support CancellationToken for improved performance in asynchronous programming.

Features

  • Generic Support: Works with any entity implementing IEntity<TKey> and any primary key type that is IEquatable<TKey>.
  • Asynchronous Operations: All methods are asynchronous, ensuring efficiency.
  • Common CRUD Operations:
    • Fetching data with optional predicates.
    • Checking for the existence of entities.
    • Inserting, updating, and deleting single or multiple items.
  • No-Tracking Queries: Provides support for read-only queries with .AsNoTracking for improved performance when updates are not needed.
  • SQL Execution: Executes raw SQL commands with ExecuteSqlRawAsync.

Methods Overview

Public Methods

  • Read Operations:

    • GetAllAsync(): Fetches all entities or those matching a predicate.
    • GetAsync(): Retrieves a single entity by ID.
    • AnyAsync(): Checks if an entity with a given ID exists.
  • Create Operations:

    • InsertAsync(): Inserts a single entity.
    • InsertRangeAsync(): Inserts multiple entities.
  • Update Operations:

    • UpdateAsync(): Updates a single entity.
    • UpdateRangeAsync(): Updates multiple entities.
  • Delete Operations:

    • DeleteAsync(): Deletes a single entity.
    • DeleteRangeAsync(): Deletes multiple entities.
  • SQL Execution:

    • ExecuteSqlRawAsync(): Executes raw SQL commands.

Protected Members

  • Context and DbSet:

    • _context: The DbContext instance.
    • _entities: The DbSet<TEntity> representing the entity's table.
  • Save Changes:

    • SaveChangesAsync(): Commits changes to the database asynchronously.

How to Use

  1. Extend the Repository:

    • Create a repository class that derives from EfRepository and provide the specific entity and key types:
      public class UserRepository : EfRepository<UserEntity, Guid>
      {
          public UserRepository(ApplicationDbContext context) : base(context) { }
      }
      
  2. DI Configuration:

    • Register your repository with your DI container to use it across the application:
      services.AddScoped<IRepository<UserEntity, Guid>, UserRepository>();
      
  3. Access the Repository:

    • Use the injected repository to perform CRUD operations:
      var users = await userRepository.GetAllAsync();
      await userRepository.InsertAsync(newUser);
      

Generic Constraints

  • TEntity: Must implement IEntity<TKey> and be a class.
  • TKey: Must be IEquatable<TKey>, ensuring compatibility with EF Core requirements for keys.

Example Use Case

For an entity Product with a Guid as the primary key:

public class Product : IEntity<Guid>
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

// Repository
public class ProductRepository : EfRepository<Product, Guid>
{
    public ProductRepository(MyDbContext context) : base(context) { }
}

// Usage
var products = await productRepository.GetAllAsync(p => p.Name.StartsWith("A"));
await productRepository.InsertAsync(new Product { Id = Guid.NewGuid(), Name = "Apple" });

Notes

  • Ensure proper exception handling for methods throwing ArgumentNullException and NullReferenceException.
  • Use AsNoTracking for scenarios where entities do not require change tracking, improving query performance.

Dependencies

  • NuGet Packages:
    • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.Abstractions

License

No explicit license available. Adapt and use responsibly in compliance with your project’s licensing guidelines.

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. 
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.3 318 2/23/2025
1.0.2 82 2/23/2025
1.0.1 93 2/23/2025