AlwaysDeveloping.EntityFrameworkCore.DynamicContext 1.0.0

dotnet add package AlwaysDeveloping.EntityFrameworkCore.DynamicContext --version 1.0.0                
NuGet\Install-Package AlwaysDeveloping.EntityFrameworkCore.DynamicContext -Version 1.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="AlwaysDeveloping.EntityFrameworkCore.DynamicContext" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AlwaysDeveloping.EntityFrameworkCore.DynamicContext --version 1.0.0                
#r "nuget: AlwaysDeveloping.EntityFrameworkCore.DynamicContext, 1.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 AlwaysDeveloping.EntityFrameworkCore.DynamicContext as a Cake Addin
#addin nuget:?package=AlwaysDeveloping.EntityFrameworkCore.DynamicContext&version=1.0.0

// Install AlwaysDeveloping.EntityFrameworkCore.DynamicContext as a Cake Tool
#tool nuget:?package=AlwaysDeveloping.EntityFrameworkCore.DynamicContext&version=1.0.0                

DynamicContext

Provides support for executing raw SQL queries against a Entity Framework Core DbContext without a DbSet for the entity. Itt also provides support to return a result set of single types (int, bool, Guid, string etc).

Installing AlwaysDeveloping.EntityFrameworkCore.DynamicContext

AlwaysDeveloping.EntityFrameworkCore.DynamicContext is available on NuGet via:

Install-Package AlwaysDeveloping.EntityFrameworkCore.DynamicContext

Or via the .NET CLI:

dotnet add package AlwaysDeveloping.EntityFrameworkCore.DynamicContext

Or via NuGet Packager Manager in Visual Studio

Direct link to AlwaysDeveloping.EntityFrameworkCore.DynamicContext on NuGet: https://www.nuget.org/packages/AlwaysDeveloping.EntityFrameworkCore.DynamicContext/

How to use AlwaysDeveloping.EntityFrameworkCore.DynamicContext

Configuring the dependency injection container

When using Entity Framework Core, when adding the DbContext to the DI container, use AddDynamicContext instead of AddContext

 host = Host.CreateDefaultBuilder()
    .ConfigureServices((context, services) => services
        .AddDynamicContext<BlogContext>(x => x.UseSqlite($"Data Source={Environment
            .GetFolderPath(Environment.SpecialFolder.MyDocuments)}\\BlogDatabase.db"))
    ).Build();

Using DynamicContext<T>

Inject DynamicContext<T> (where T is the underlying DbContext) into the relevant class.

Using the DynamicContext to retrieve data into an entity (or list of entities), without having a DbSet<>:

// Using the original underlying dbContext
// DbSet<Blog> must exists on the context
var blogs = dynContext.Context.Set<Blog>().AsNoTracking().ToList();

// Retrieving data dynamically
// DbSet<BlogUrl> does NOT need to exist on the context
var blogs = dynContext.Set<BlogUrl>()
    .FromSqlRaw("SELECT Id as BlogId, Url FROM Blog")
    .AsNoTracking().ToList();

// Retrieving data using anonymous object as a 'template' for the result
var anonBlogUrl = new { BlogId = 0, Url = "" };
var blogs = dynContext.Set(anonBlogUrl)
    .FromSqlRaw("SELECT Id as BlogId, Url FROM Blog")
    .AsNoTracking().ToList();

Using the DynamicContext to retrieve data into a simple type (or list of simple types) - simple types being value types (int, bool etc), Guid and strings:

// Retrieving Guid dynamically
// DbSet<Guid> does NOT exist on the context
var blogIds = dynContext.ValueSet<Guid>()
    .FromSqlRaw("SELECT Id as Value FROM Blog")
    .AsNoTracking().ToSimple().ToList();

// Retrieving string dynamically
// DbSet<string> does NOT exist on the context
var urls = context.StringSet<string>()
    .FromSqlRaw("SELECT Url as Value FROM Blog")
    .AsNoTracking().ToSimple().ToList();

Benchmarks

See this blog post for some simple benchmarks on comparing DbContext to DynamicContext (TLDR: DynamicContext is faster for the use cases tested)

Use cases

There are a variety of ways to retrieve the data using a DbContext - some more dynamic than others. This library address certain use cases not catered for by EF Core out the box.

The library is intended to be used in conjunction with, and complementing the EF Core DBContext functionality. If The EF Core DbContext be only being used to leverage the functionality of this library, it might make more sense to use a completely different ORM to handle the data access.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.0.0 5,923 12/12/2021