EmilianoMusso.EFCoreExtensions.DbToInMemory 1.1.0-alpha.1

This is a prerelease version of EmilianoMusso.EFCoreExtensions.DbToInMemory.
dotnet add package EmilianoMusso.EFCoreExtensions.DbToInMemory --version 1.1.0-alpha.1
NuGet\Install-Package EmilianoMusso.EFCoreExtensions.DbToInMemory -Version 1.1.0-alpha.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="EmilianoMusso.EFCoreExtensions.DbToInMemory" Version="1.1.0-alpha.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EmilianoMusso.EFCoreExtensions.DbToInMemory --version 1.1.0-alpha.1
#r "nuget: EmilianoMusso.EFCoreExtensions.DbToInMemory, 1.1.0-alpha.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.
// Install EmilianoMusso.EFCoreExtensions.DbToInMemory as a Cake Addin
#addin nuget:?package=EmilianoMusso.EFCoreExtensions.DbToInMemory&version=1.1.0-alpha.1&prerelease

// Install EmilianoMusso.EFCoreExtensions.DbToInMemory as a Cake Tool
#tool nuget:?package=EmilianoMusso.EFCoreExtensions.DbToInMemory&version=1.1.0-alpha.1&prerelease

EmilianoMusso.EFCoreExtensions.DbToInMemory

A library to easy load persisted data from database to InMemory

Usage:

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>()
    .UseInMemoryDatabase("TEST");

var context = new MyDbContext(optionsBuilder.Options);

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

toInMem.LoadTable<MyTableType>()
       .LoadTable<AnotherTableType>(5)
       .PersistToMemory();

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.

v1.1.0-alpha.1

From this version on, it is possible to specify an expression to select entities from tables. The LINQ expression will be translated to SQL syntax. A sample of this process can be seen in the original repository, on the test project, and can be resumed like the following.

var linqExpression = "x => x.Property01.Contains(\"A\") AndAlso x.Property02 == 1";
var expectedClause = "WHERE Property01 LIKE '%A%' AND Property02 = 1";

var result = LinqFuncToSqlLangHelper.GetSQLWhereClause(linqExpression);
result.Should().Be(expectedClause);

LoadTable<T> method could thus be implemented with a selector, like this:

toInMem.LoadTable<MyTableType>()
       .LoadTable<AnotherTableType>(x => x.Property01 > 100, topRecords: 5)
       .LoadTable<ThirdType>(x => x.StringProp.StartsWith("E") && x.TestProperty != 10, topRecords: 5)
       .PersistToMemory();

Further translation possibilities will come in future versions.

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.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.0-alpha.1 161 6/16/2021
1.0.0 417 6/12/2021