EmilianoMusso.EFCoreExtensions.DbToInMemory 1.0.0

.NET Standard 2.1
There is a newer prerelease version of this package available.
See the version list below for details.
Install-Package EmilianoMusso.EFCoreExtensions.DbToInMemory -Version 1.0.0
dotnet add package EmilianoMusso.EFCoreExtensions.DbToInMemory --version 1.0.0
<PackageReference Include="EmilianoMusso.EFCoreExtensions.DbToInMemory" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EmilianoMusso.EFCoreExtensions.DbToInMemory --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EmilianoMusso.EFCoreExtensions.DbToInMemory, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install EmilianoMusso.EFCoreExtensions.DbToInMemory as a Cake Addin
#addin nuget:?package=EmilianoMusso.EFCoreExtensions.DbToInMemory&version=1.0.0

// Install EmilianoMusso.EFCoreExtensions.DbToInMemory as a Cake Tool
#tool nuget:?package=EmilianoMusso.EFCoreExtensions.DbToInMemory&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.


A library to easy load persisted data from database to InMemory


Let's suppose we have a Sql Server instance named MyInstance, and a database named MyDatabase. The database contains tables, two of them named MyTableType and AnotherTableType. We need to use already persisted data for our integration tests, without tampering the real data, but using it.

EF Core allow us to specify a memory instanced data context using UseInMemoryDatabase method, which allow us to replicate our model in an in-memory structure. This method supports initial seeding through migrations, but this extension has the purpose to make easier the in-memory data seeding.

Please consider the following snippet:

var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>()

var context = new MyDbContext(optionsBuilder.Options);

var connectionString = @"Server=.\MyInstance;Database=MyDatabase;Trusted_Connection=True;";
var toInMem = new DatabaseToInMemory(context, connectionString);


Here, we have declared an in-memory database modeled on an already existant DbContext. Then, having instantiated our context, we can simply initialize a new object of DatabaseToInMemory type.

That class must be initialized by passing a DbContext and a connection string to a physical database. Afterwards, we can use the LoadTable<T> method, where <T> is one of the types present in our context to indicate the DbSets representing each table. That method accepts a parameter to specify a number of record to be loaded (10 by default).

Unless otherwise stated in the DatabaseToInMemory constructor, data will be picked randomly from each table. At the end of load operations, which can be concatenated, we can call PersistToMemory or PersistToMemoryAsync - which are two wrappers for SaveChanges and SaveChangesAsync - to persist our data in the in-memory database.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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.0-alpha.1 102 6/16/2021
1.0.0 246 6/12/2021