EfCore.InMemoryHelpers Save

Wrapper around the EF Core In-Memory DB to work around some qwerks

Project README

EfCore.InMemoryHelpers

Actively Maintained Build status NuGet Status License: MIT

Provides a wrapper around the EF Core In-Memory Database Provider. Specifically works around the following EF bugs.

  • InMemory: Improve in-memory key generation
    Reasoning: For many bootstrapping and integration tests, the id generation should be predictable, i.e. an in-memory persistence should not share static mutable state. This is especially important when using unit tests as a bootstrap to generate ad-hoc data.
  • Add index validation
    Reasoning: It is desirable for indexes to be validated when running unit tests. This allows bugs to be caught earlier without the need for integration testing against a real database.
  • Add support for Timestamp/row version ([Timestamp], and .Property(p => p.X).IsRowVersion())
    Reasoning: It is desirable for exceptions to be thrown when a update violates a RowVersion. This allows bugs to be caught earlier without the need for integration testing against a real database.

This project is supported by the community via Patreon sponsorship. If you are using this project to deliver business value or build commercial software it is expected that you will provide support via Patreon.

NuGet

https://nuget.org/packages/EfCore.InMemoryHelpers/

PM> Install-Package EfCore.InMemoryHelpers

Usage

The main entry point is InMemoryContextBuilder which can be used to build an in-memory context.

using (var context = InMemoryContextBuilder.Build<MyDataContext>())
{
    var entity = new MyEntity
    {
        Property = "prop"
    };
    context.Add(entity);
    context.SaveChanges();
}

snippet source

A custom DbContextOptionsBuilder can be passed in:

var builder = new DbContextOptionsBuilder<MyDataContext>();
using (var context = InMemoryContextBuilder.Build<MyDataContext>(builder))
{
    var entity = new MyEntity
    {
        Property = "prop"
    };
    context.Add(entity);
    context.SaveChanges();
}

snippet source

Both the above usages assume that the target context has a public constructor that accepts a DbContextOptions.

public MyDataContext(DbContextOptions options)
    :
    base(options)
{ }

snippet source

If this is not the case a custom context constructor can be passed in:

var builder = new DbContextOptionsBuilder<MyDataContext>();
using (var context = InMemoryContextBuilder.Build(builder, options => new MyDataContext(options)))
{
    var entity = new MyEntity
    {
        Property = "prop"
    };
    context.Add(entity);
    context.SaveChanges();
}

snippet source

Icon

memory designed by Montu Yadav from The Noun Project

Open Source Agenda is not affiliated with "EfCore.InMemoryHelpers" Project. README Source: FelixBoers/EfCore.InMemoryHelpers
Stars
71
Open Issues
6
Last Commit
3 years ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating