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

// Install Com.H.EF.Relational as a Cake Tool
#tool nuget:?package=Com.H.EF.Relational&version=


Adds native raw SQL support to EntityFrameworkCore without the need for DbSet.

Sample 1

This sample demonstrates how to execute a simple query without parameters on a SQL Server Database.

To run this sample, you need to:

  1. Create a new console application
  2. Add NuGet package Com.H.EF.Relational
  3. Add NuGet package Microsoft.EntityFrameworkCore.SqlServer
  4. copy and paste the following code into your Program.cs file:
using Com.H.EF.Relational;
using Microsoft.EntityFrameworkCore;

string conStr = "connection string goes here";
DbContext dc = conStr.CreateDbContext();

var result = dc.ExecuteQuery("select 'abc' as name, '123' as phone");
// ^ returns IEnumerable<dynamic>, you can also return IEnumerable<T> where T is your data model class
// by using the ExecuteQuery<T> method which returns IEnumerable<T>
// example: var result = dc.ExecuteQuery<YourDataModelClass>("select 'abc' as name, '123' as phone");
// Also, returns IAsyncEnumerable when called asynchronously via dc.ExecuteQueryAsync()

foreach(var item in result)
	System.Console.WriteLine($"name = {}, phone = {}");

Sample 2

This sample demonstrates how to pass parameters to your SQL query

using Com.H.EF.Relational;
using Microsoft.EntityFrameworkCore;

string conStr = "connection string goes here";
DbContext dc = conStr.CreateDbContext();
var queryParams = new { name = "abc" };

var result = dc.ExecuteQuery(@"
	select * from (values ('abc', '123'), ('def', '456')) as t (name, phone)
	where name = {{name}}", queryParams

// ^ queryParams could be an anonymous object (similar to the example above)
// a normal object, or IDictionary<string, object>
// Example 1: var queryParams = new Dictionary<string, object> { { "name", "abc" } }
// Example 2: var queryParams = new CustomParamClass { name = "abc" }

foreach(var item in result)
	System.Console.WriteLine($"name = {}, phone = {}");

What other databases this library supports?

Any EntityFrameworkCore database, just add your target database NuGet package, create a DbContext and use the extension methods offered by Com.H.EF.Relational to execute any SQL queries you want.

How does conStr.CreateDbContext() method in the samples above work?

CreateDbContext() does a lookup for either Microsoft.EntityFrameworkCore.SqlServer or Microsoft.EntityFrameworkCore.SqlLite packages in your project and creates a DbContext object accordingly for whichever package (SqlServer or SqlLite) it finds.

If it detects both packages are added in your project, it'll create a DbContext for SQL Server. To override this behavior, i.e. create a DbContext for SqlLite instead of SqlServer (in the event of both packages are present in your project) use conStr.CreateDbContext("Microsoft.EntityFrameworkCore.SqlLite") instead of just conStr.CreateDbContext()

If you have a different database other than SqlServer or SqlLite, just create a DbContext the normal way you would do usually (i.e. without the use of CreateDbContext()) and your DbContext object should have all the extension methods offered by Com.H.EF.Relational package available to it.

CreateDbContext() is written just to simplify the DbContext creation part, it's not essential by any means. Adding support in CreateDbContext() for other than SqlServer & SqlLite DbContext creation should be fairly straight forward (takes few minutes per database) and I will hopefully look into adding most common popular databases once I get the time to lookup their corresponding assemblies & extension method calls that generates their DbContext.

What other features this library has?

This small library has several other options that allow for more advanced features that might not be of much use to most, hence samples for those features have been left out in this quick how to documentation.

Open source

This package is written in C# and is fully open source. Kindly visit the project's github page

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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 156 8/11/2023 139 6/30/2023 128 6/30/2023 339 11/14/2022
7.0.0 316 11/9/2022 364 11/8/2022 359 10/30/2022 391 10/10/2022 486 9/3/2022 383 8/28/2022 376 8/28/2022 391 8/28/2022
1.0.6 614 1/24/2022
1.0.5 434 1/21/2022
1.0.4 445 1/11/2022
1.0.2 529 5/8/2021
1.0.1 465 1/3/2021
1.0.0 462 1/2/2021

Maintenance release