EF.Core.Repositories.Test 1.1.1

dotnet add package EF.Core.Repositories.Test --version 1.1.1
                    
NuGet\Install-Package EF.Core.Repositories.Test -Version 1.1.1
                    
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="EF.Core.Repositories.Test" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EF.Core.Repositories.Test" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="EF.Core.Repositories.Test" />
                    
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 EF.Core.Repositories.Test --version 1.1.1
                    
#r "nuget: EF.Core.Repositories.Test, 1.1.1"
                    
#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 EF.Core.Repositories.Test@1.1.1
                    
#: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=EF.Core.Repositories.Test&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=EF.Core.Repositories.Test&version=1.1.1
                    
Install as a Cake Tool

EF.Core.Repositories

Extends Entity Framework Core to provide a Repository Pattern based on LINQ.

Installing

Install NuGet package from NuGet.org

$ dotnet add package EF.Core.Repositories

Requirements

  • .NET 7 or higher
  • Entity Framework Core 7.0.14+

Getting Started

The Repository Factory can be registered by calling ConfigureData() extension.

The Action<DbContextOptionsBuilder> will be passed to AddDbContextFactory() extension provided by Entity Framework Core.

Note Due to the support for multiple asynchronous actions being executed against one or more repositories, multiple contexts may be created, and leveraging a DbContextFactory is required.

using EF.Core.Repositories;
...
services.ConfigureData<TContext>(opt => ...);

Within a Service/Controller/etc.

public class Consumer
{
  private readonly IRepositoryFactory<TContext> _factory;

  public Consumer(IRepositoryFactory<TContext> factory)
  {
    _factory = factory;
  }

  ...

  public async Task Command()
  {
    var repository = _factory.GetRepository<TEntity>();
    ...
  }
}

Usage

IReadOnlyRepository LINQ Extensions

The majority of IQueryable Extensions are also available for IReadOnlyRepository.

  • Distinct()
  • Except()
  • GroupBy()
  • Intersect()
  • IntersectBy()
  • Join()
  • OrderBy()
  • OrderByDescending()
  • Select()
  • SelectMany()
  • Skip()
  • SkipLast()
  • SkipWhile()
  • Take()
  • TakeLast()
  • TakeWhile()
  • ThenBy()
  • ThenByDescending()
  • Union()
  • UnionBy()
  • Where()
  • Zip()

IReadOnlyRepository Async Extensions

Additional Extensions that will enumerate the Repository against the DbContext.

  • AllAsync()
  • AnyAsync()
  • AverageAsync()
  • CountAsync()
  • FirstAsync()
  • FirstOrDefaultAsync()
  • GetAsync()
    • Returns an IEnumerable containing the entities of the Repository.
  • LastAsync()
  • LastOrDefaultAsync()
  • LongCountAsync()
  • MaxAsync()
  • MinAsync()
  • SingleAsync()
  • SingleOrDefaultAsync()
  • SumAsync()

IRepository Extensions

IRepository has all the extensions available to IReadOnlyRepository and adds the following.

  • Include()
  • ThenInclude()
Include()

Just like when working with a DbSet exposed by a DbContext, you can include loading navigation properties of your entities.

Ex. Retrieving all Users, their UserRoles, and the subsequent referenced Role

var repository = _factory
  .GetRepository<User>()
  .Include(x => x.UserRoles)
  .ThenInclude(x => x.Role);

var users = await repository.GetAsync();

IRepository Async Extensions

IRepository has all the async extensions available to IReadOnlyRepository and adds the following.

  • DeleteAsync(entity)
  • DeleteByIdAsync(key)
  • GetAsync(key)
  • InsertAsync(entity)
  • UpdateAsync(entity)
GetAsync(key) and DeleteByIdAsync(key)

These functions allow you to retreive/remove an entity based upon primary key data.

Ex. Retrieving a User with a simple primary key of (int UserId)

var repository = _factory.GetRepository<User>();

var user = await repository.GetAsync(new { UserId = 5 });

Ex. Retrieving a UserRole with a complex primary key of (int UserId, int RoleId)

var repository = _factory.GetRepository<UserRole>();

var userrole = await repository.GetAsync(new { UserId = 5, RoleId = 1 });
UpdateAsync(entity)

By default UpdateAsync will ignore all navigation properties and only update the concrete data fields within the entity.

To include a navigation property as part of the update use the Include() extension.

Ex. Adding a UserRole to a User

var repository = _factory.GetRepository<User>();

var user = await repository
  .Include(x => x.UserRoles)
  .GetAsync(new { UserId = 5 });

user.UserRoles.Add(
  new UserRole
  {
    UserId = 5,
    RoleId = 2,
    ...
  });

var result = await repository
  .Include(x => x.UserRoles)
  .UpdateAsync(user);

Transactions

Normally calls to InsertAsync or UpdateAsync immediately call SaveChangesAsync on the backing context and commit the changeset to the database.

Transactions allow for multiple changes to be committed to the database within a single SaveChangesAsync.

Example

Inserting multiple users.

using var transaction = _factory.CreateTransaction();

var repository = transaction.GetRepository<User>();

foreach (var user in users)
{
    await repository.InsertAsync(user);
}

var inserted = await transaction.CommitAsync();

or

using var transaction = _factory.CreateTransaction();

var repository = transaction.GetRepository<User>();

await Task.WhenAll(
  users.Select(async user =>
    await repository.InsertAsync(user)));

var inserted = await transaction.CommitAsync();

Testing

The EF.Core.Repositories.Test package can be used to facilitate testing.

Supported Providers

Currently there are three data providers supported.

  • InMemory
    • Utilizes the EntityFrameworkCore.InMemory provider
  • Sqlite
    • Utilizes the EntityFrameworkCore.Sqlite provider with in memory Sqlite instances
  • Sql (Recommended)
    • Utilizes the EntityFrameworkCore.SqlServer provider
    • A connection string must be passed to the builder method for use during tests
      • Initial Catalog parameter will be ignored if included
    • User must have create database permissions on the server
    • Each time CreateFactoryAsync() is called a new database on configured server is created
    • Sql Factories will automatically delete the created database when disposed.
    • Failed tests will not trigger .Dispose() when factory is wrapped in a using context.
      • This will cause that database to survive and be available to aid in debugging test failure.

FactoryBuilders

Each provider has an IFactoryBuilder<T> which uses a seed function to specify all objects that should be stored in the data source when it is initialized.

Best practice is to initialize a builder in your constructor and then create a factory to start each test. This will result in each test using its own data source.

Example

Testing a UserController's Add/Update endpoints

public class UserControllerTests
{
    private readonly IFactoryBuilder<MyContext> _builder;

    public UserControllerTests()
    {
        _builder = IFactoryBuilder<MyContext>.Sql("Server=(local);Integrated Security=true;TrustServerCertificate=true",
            () => new object[]
            {
                new User
                {
                    Id = 1,
                    Name = "Test User",
                    Email = "user@test.com",
                }
            });
    }

    [Fact]
    public async void AddUser()
    {
        using var factory = await _builder.CreateFactoryAsync();

        var controller = new UserController(factory);

        var user = new User
        {
            Id = 2,
            Name = "Test User 2",
            Email = "user2@test.com",
        };

        var result = await controller.Add(user);

        Assert.NotNull(result);
        Assert.Equal(200, result.StatusCode);
    }

    [Fact]
    public async void UpdateUser()
    {
        using var factory = await _builder.CreateFactoryAsync();

        var controller = new UserController(factory);

        var user = new User
        {
            Id = 1,
            Name = "Test User 1",
            Email = "user1@test.com",
        };

        var result = await controller.Update(user);

        Assert.NotNull(result);
        Assert.Equal(200, result.StatusCode);
    }

}
Product 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.  net9.0 was computed.  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.1.1 152 4/19/2024
1.1.0 122 4/19/2024