SimpleEfCoreRepository 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SimpleEfCoreRepository --version 3.0.0
NuGet\Install-Package SimpleEfCoreRepository -Version 3.0.0
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="SimpleEfCoreRepository" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleEfCoreRepository --version 3.0.0
#r "nuget: SimpleEfCoreRepository, 3.0.0"
#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 SimpleEfCoreRepository as a Cake Addin
#addin nuget:?package=SimpleEfCoreRepository&version=3.0.0

// Install SimpleEfCoreRepository as a Cake Tool
#tool nuget:?package=SimpleEfCoreRepository&version=3.0.0

EF.Core.Repository

Simple repository for Ef.Core with basic CRUD functionality The reason I implemented this is because I found myself re-writing basic CRUD functionality over and over again. Most of the time:

  • use .include(...) to include eager load associated entities
  • update (add/delete/update) children properties

Using repository pattern with entity framework enforces a consistent convention and that is what this library is aiming towards.

NuGet Status Build Status

Basic setup
  • Entity should have Id property. Using [Key] is optional if Id property does not follow common naming convention.
public class DummyModel
{
    public string Name { get; set; }

    public List<Nested> Children { get; set; }
}
  • Create profile which is used to update an entity given a DTO
public class DummyModelProfile : EntityProfile<DummyModel> 
{
    public void Update(DummyModel entity, DummyModel dto)
    {
        entity.Name = dto.Name;

        // ModifyList will try to add/delete entities based on Id based on whether they
        // appear in dto.Children or not 
        ModifyList(entity.Children, dto.Children, x => x.Id);
    }

    // Intercept IQueryable to include related entities
    public IQueryable<DummyModel> Include<TQueryable>(TQueryable queryable) where TQueryable : IQueryable<DummyModel>
    {
        return queryable.Include(x => x.Children);
    }
}
  • Register dependency via IServiceCollection extension
var serviceProvider = services
    .AddEfRepository<EntityDbContext>(options => options
        .Profile(Assembly.GetExecutingAssembly()));
  • Use IBasicCrud
IEfRepository repo = ... // DI inject IEfRepository

// Get IBasicCrud instance
IBasicCrud<DummyModel> = repo.For<DummyModel>(dbContext);
  • Available methods in IBasicCrud
// Get all entities without any filter
Task<IEnumerable<TSource>> GetAll();

// Get all entities given an array of Ids
Task<IEnumerable<TSource>> GetAll<TId>(param TId[]);

// Get all entities given a filter expression
Task<IEnumerable<TSource>> GetAll(Expression<Func<TSource, bool>>);

// Get single entity by Id
Task<TSource> Get<TId>(TId);

// Get single entity given a filter expression
Task<TSource> Get(Expression<Func<TSource, bool>>);

// Save entity
Task<TSource> Save(TSource);

// Save multiple entities
Task<IEnumerable<TSource>> Save(param TSource[]);

// Update entity by Id
Task<TSource> Update<TId>(TId, TSource);

// Update entity by filter expression
Task<TSource> Update(Expression<Func<TSource, bool>>, TSource);

// Delete entity by Id
Task<TSource> Delete<TId>(TId);

// Delete entity given a filter expression
Task<TSource> Delete(Expression<Func<TSource, bool>>);

// This is useful if you want to defer SaveChanges in session mode
// Changes are not automatically saved back to DbContext in session mode
IBasicCrud<TSource, TId> Delayed();

// This is useful if you want don't want to include all related entities
// or turn entities into a "god" object
IBasicCrud<TSource, TId> Light();

Notes:

  • "Id" has a type constraint of : struct, which means it accepts all primitive types including GUID and String.
  • Including all associated entities will lead to "god object" which is anti-pattern. I recommend using this library as L2 cache
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
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
3.0.14 285 4/28/2023
3.0.13 203 4/16/2023
3.0.12 165 4/16/2023
3.0.11 229 3/21/2023
3.0.10 229 3/9/2023
3.0.9 216 3/9/2023
3.0.8 411 10/13/2022
3.0.7 455 9/18/2022
3.0.6 427 9/10/2022
3.0.5 400 9/10/2022
3.0.4 400 9/10/2022
3.0.3 413 9/8/2022
3.0.2 412 9/8/2022
3.0.1 672 7/12/2022
3.0.0 503 6/20/2022
2.1.5 461 6/20/2022
2.1.4 424 4/19/2021
2.1.3 380 4/19/2021
2.1.2 337 4/18/2021
2.1.1 416 4/17/2021
2.1.0 401 4/17/2021
2.0.2 421 1/2/2021
2.0.1 449 1/2/2021
2.0.0 393 12/28/2020
1.0.8 465 11/5/2020
1.0.7 474 8/16/2020
1.0.6 448 8/10/2020
1.0.5 438 8/10/2020
1.0.4 510 6/20/2020
1.0.3 478 6/20/2020
1.0.2 442 6/17/2020
1.0.1 516 5/25/2020
1.0.0 540 5/24/2020

Restructured to be more compliant with lifetime of DbContext.