249

With Entity Framework Core removing dbData.Database.SqlQuery<SomeModel> I can't find a solution to build a raw SQL Query for my full-text search query that will return the tables data and also the rank.

The only method I've seen to build a raw SQL query in Entity Framework Core is via dbData.Product.FromSql("SQL SCRIPT"); which isn't useful as I have no DbSet that will map the rank I return in the query.

Any Ideas???

steamrolla
  • 2,373
  • 1
  • 29
  • 39
David Harlow
  • 2,673
  • 2
  • 15
  • 18
  • 54
    I will greatly miss the SqlQuery and don't want to have to map custom classes to my DbContext when I really just need a simple DTO for a specific use case. I have created a user voice to request adding this feature back in to EF Core that anyone can vote up if they would like this feature back: https://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/13183638-add-dbcontext-database-sqlquery-to-entity-framewor – Matt Sanders Mar 28 '16 at 16:35
  • 1
    According to https://github.com/aspnet/EntityFramework/issues/1862, this is now targetted for EF core 1.2 and/or 1.1.0-preview1 – Dan Field Nov 17 '16 at 00:22
  • An important note, FromSql and ExecuteSqlCommand are extension methods, they don't actually exist in DbSet or DatabaseFacade, so you'll need to make sure the namespace Microsoft.EntityFrameworkCore is imported. – Devon Bessemer Aug 21 '19 at 14:33
  • 2
    Building on what @Devon just said, I spent way too long just now figuring out they are extension methods in Microsoft.EntityFrameworkCore.SqlServer. You'll need to add that to your project before getting these extension methods. – Daniel Sep 12 '19 at 16:50
  • 11
    *Sigh* this seems like some kind of Architecture Astronaut decision: "the people shouldn't need to want this". I guess I have to install Dapper just for this case. Annoying. – Dirk Boer Apr 29 '20 at 12:25
  • 3
    @MattSanders - you're uservoice link seems to be dead in the meantime. Do you know where it went? – Dirk Boer Apr 29 '20 at 12:28
  • 2
    I have also installed Dapper just for this, using an extension method over the DbContext and it worked great. – Augusto Barreto Sep 22 '21 at 18:25
  • In April 2022, the following let me run a raw sql command: using Microsoft.EntityFrameworkCore; myDbContext.Database.ExecuteRawSQL("your fancy sql"); – TomEberhard Apr 05 '22 at 00:48

21 Answers21

251

Support for raw SQL queries returning scalar values was added in EF Core 7 via the new SqlQuery method. That method has been enhanced to support querying arbitrary types in EF Core 8, so when the latter is released we'll finally have parity with EF 6.


If you're using EF Core 3.0 or newer

You need to use keyless entity types, previously known as query types:

This feature was added in EF Core 2.1 under the name of query types. In EF Core 3.0 the concept was renamed to keyless entity types. The [Keyless] Data Annotation became available in EFCore 5.0.

To use them you need to first mark your class SomeModel with [Keyless] data annotation or through fluent configuration with .HasNoKey() method call like below:

public DbSet<SomeModel> SomeModels { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<SomeModel>().HasNoKey();
}

After that configuration, you can use one of the methods explained here to execute your SQL query. For example you can use this one:

var result = context.SomeModels.FromSqlRaw("SQL SCRIPT").ToList();
var result = await context.SomeModels.FromSql("SQL_SCRIPT").ToListAsync();

If you're using EF Core 2.1

If you're using EF Core 2.1 Release Candidate 1 available since 7 may 2018, you can take advantage of the proposed new feature which is query types:

In addition to entity types, an EF Core model can contain query types, which can be used to carry out database queries against data that isn't mapped to entity types.

When to use query type?

Serving as the return type for ad hoc FromSql() queries.

Mapping to database views.

Mapping to tables that do not have a primary key defined.

Mapping to queries defined in the model.

So you no longer need to do all the hacks or workarounds proposed as answers to your question. Just follow these steps:

First you defined a new property of type DbQuery<T> where T is the type of the class that will carry the column values of your SQL query. So in your DbContext you'll have this:

public DbQuery<SomeModel> SomeModels { get; set; }

Secondly use FromSql method like you do with DbSet<T>:

var result = context.SomeModels.FromSql("SQL_SCRIPT").ToList();
var result = await context.SomeModels.FromSql("SQL_SCRIPT").ToListAsync();

Also note that DbContexts are partial classes, so you can create one or more separate files to organize your 'raw SQL DbQuery' definitions as best suits you.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • 30
    This answer should be the best solution when using EF Core 2.1 and above. – Will Huang Aug 13 '18 at 18:55
  • 3
    @CodeNotFound What if I don't need the result or if it's a primitive type (for example `bit`)? – Shimmy Weitzhandler Dec 24 '18 at 09:42
  • 1
    @Shimmy you can check my answer [here](https://stackoverflow.com/questions/50402015/how-to-execute-sqlquery-with-entity-framework-core-2-1/50409786#50409786) – CodeNotFound Dec 25 '18 at 12:23
  • 1
    Can I do nested objects? – Toolkit Aug 02 '19 at 16:04
  • @Toolkit yep. You can use nested type no problems. – CodeNotFound Aug 02 '19 at 16:04
  • 1
    @CodeNotFound can you please point me to some examples. Having hard time finding them – Toolkit Aug 02 '19 at 16:09
  • @Toolkit to make sure, are you talking about this types => https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/nested-types ? – CodeNotFound Aug 02 '19 at 16:10
  • 1
    no sorry, I just want to map `select c.Id, c.Name, l.Id, l.Name from course c join location l on c.locationid = l.id` to a `CourseViewMoodel` where `CourseViewMoodel` has a `List` – Toolkit Aug 02 '19 at 16:14
  • @Toolkit don't use view model into your DbContext. Just create a new type in your domain entities then map it back to your ViewModel. – CodeNotFound Aug 02 '19 at 16:18
  • 1
    @CodeNotFound yeah I got it, but still how to map a query to a complex object? – Toolkit Aug 02 '19 at 16:19
  • @Toolkit can you please create a new question with lire détails about your issue please. – CodeNotFound Aug 02 '19 at 16:20
  • 1
    @CodeNotFound the way EF is doing it magically. But I want to feed my own SQL but get an automatic mapping – Toolkit Aug 02 '19 at 16:20
  • @Toolkit so check thie [answer](https://stackoverflow.com/questions/50402015/how-to-execute-sqlquery-with-entity-framework-core-2-1/50409786#50409786) I gave there how to map your custom SQL with DbQuery and your Type. – CodeNotFound Aug 02 '19 at 16:22
  • 13
    Using CodeFirst this automatically created a table with all those properties, adding `[NotMapped]` to the `SomeModels` class does not work for me. Did I miss anything? – Jean-Paul Sep 20 '19 at 11:51
  • 9
    EF Core 3.0 deprecates `DbQuery` in favor of just using `DbSet` with [keyless entity types](https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types). – NetMage Nov 12 '19 at 23:51
  • 1
    @NetMage, the function you said is just for mapping the entity without primary key – Ray Nov 28 '19 at 03:44
  • 1
    @Ray true, but it is not restricted to that - `DbQuery` is replaced by `DbSet` in all ways in EF Core 3.0. – NetMage Dec 02 '19 at 20:11
  • 1
    https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbquery-1?view=efcore-3.1 This is deprecated. – Greg R Taylor Apr 08 '20 at 10:38
  • 29
    Just FYI, due to some bug in EF core 3.0, a code-first migration will still try to create a table even on entities marked with HasNoKey(). So you have to do also add .ToView(null). E.g. `modelBuilder.Entity().HasNoKey().ToView(null);` @Jean-Paul I think this solves your issue – stann1 Jun 10 '20 at 12:13
  • 4
    The comment from stann1 about ToView should be added to actual answer, would save a lot of time for a lot of people. – Olaj Sep 02 '20 at 06:32
  • 1
    This however creates an issue when I use query on my dbcontext dbset. Relation does not exist exception is what i get. I used this solution to let my partition creation done manually via raw sql in migration, as postgres (npgsql provider) does not provide partitioning feature via efcore. How can I make sure that my queries are not affected by this statement? – kuldeep Sep 22 '20 at 17:13
  • 7
    So to run a Sql query without a DbSet you need to define a DbSet? Got it, thanks. – Egor Pavlikhin Dec 18 '20 at 01:55
  • Issue is being tracked here https://github.com/dotnet/efcore/issues/10753 – Egor Pavlikhin Dec 18 '20 at 02:34
  • I tried it but got A Command is already in progress if scoped service is used. This exception occurs even if same DbContext is used. Posted in https://stackoverflow.com/questions/66191043/how-to-use-npgsql-ef-provider-as-scoped-service – Andrus Feb 14 '21 at 10:19
  • 2
    I'm not really sure what happened but in EF Core 5.0.3 This no longer works... even with the modelBuilder.Entity().HasNoKey().ToView(null); It still tries to create the table with code first. For a minute I thought modelBuilder.Ignore(); that I found on another suggestion would work but it is a no go. Frusterating, how much I miss Database.SqlQuery It was simple and most of all it just worked! Has anyone still having this same issue found a work around? – Anthony Griggs Feb 24 '21 at 13:18
  • 1
    @AnthonyGriggs Having the same issue but unfortunately got no workaround :( – Daniel Feb 26 '21 at 07:08
  • 9
    @AnthonyGriggs version 5 support this ... modelBuilder.Entity().ToTable("ApplicationUsers", t => t.ExcludeFromMigrations()); – daremachine Feb 28 '21 at 15:22
  • 1
    @Daniel EF 5+ has breaking changes for views https://learn.microsoft.com/cs-cz/ef/core/what-is-new/ef-core-5.0/breaking-changes – daremachine Feb 28 '21 at 15:29
  • 1
    thanks so much I've been pulling my hair out all day trying to figure this out.. modelBuilder.Entity().HasNoKey().ToView(null); worked perfectly for me – punkologist Dec 02 '21 at 20:45
  • Is there a way to use .sql file as a parameter. My scripts are little bulky. – Pratap Singh Mehra Aug 16 '22 at 07:25
  • marked this down because this requires a given entity / type – Rick Penabella Oct 13 '22 at 13:16
  • 1
    To compliment this case... EF Core 7.0 has introduced back the regular SqlQuery. https://learn.microsoft.com/en-us/ef/core/querying/sql-queries – mrbitzilla Dec 05 '22 at 14:58
  • How would we do this if the SP returns multiple result sets? Any way to have them mapped like they support in EF 6 https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/advanced/multiple-result-sets ? – tasin95 Feb 21 '23 at 10:52
83

Building on the other answers I've written this helper that accomplishes the task, including example usage:

public static class Helper
{
    public static List<T> RawSqlQuery<T>(string query, Func<DbDataReader, T> map)
    {
        using (var context = new DbContext())
        {
            using (var command = context.Database.GetDbConnection().CreateCommand())
            {
                command.CommandText = query;
                command.CommandType = CommandType.Text;

                context.Database.OpenConnection();

                using (var result = command.ExecuteReader())
                {
                    var entities = new List<T>();

                    while (result.Read())
                    {
                        entities.Add(map(result));
                    }

                    return entities;
                }
            }
        }
    }

Usage:

public class TopUser
{
    public string Name { get; set; }

    public int Count { get; set; }
}

var result = Helper.RawSqlQuery(
    "SELECT TOP 10 Name, COUNT(*) FROM Users U"
    + " INNER JOIN Signups S ON U.UserId = S.UserId"
    + " GROUP BY U.Name ORDER BY COUNT(*) DESC",
    x => new TopUser { Name = (string)x[0], Count = (int)x[1] });

result.ForEach(x => Console.WriteLine($"{x.Name,-25}{x.Count}"));

I plan to get rid of it as soon as built-in support is added. According to a statement by Arthur Vickers from the EF Core team it is a high priority for post 2.0. The issue is being tracked here.

pius
  • 2,344
  • 1
  • 23
  • 25
  • 3
    Can you also add parameters in a safe way? – Giox Feb 10 '21 at 10:54
  • 1
    Is your helper obsolete since HasNoKey/ Keyless entity types are available since EF core 3.1 https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations? – Michael Freidgeim Apr 28 '21 at 13:58
  • 1
    would be great if you could update your post if there is a built in method for this now – Yannic Hamann Jun 19 '21 at 05:03
  • you should be disposing both the command and the connection... they both implement IDisposable – Mick Apr 11 '22 at 07:40
  • 1
    made this into DBContext extension, so removed the using on db context, and added DbParameter array at the end, this is great! – Rick Penabella Oct 13 '22 at 13:17
  • @Mick [not always](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.getdbconnection?view=efcore-7.0) – Xriuk May 05 '23 at 12:12
  • @Xriuk if you're instantiating objects which implement IDisposable and Dispose is never called on the object, it's pretty certain your application is going to leak resources. – Mick May 09 '23 at 00:35
  • @Mick if it's not you who instantiate them it's not you who should dispose them, even if they implement `IDisposable`. You are calling `GetDbConnection()`, not something like `CreateConnection()`, even if it implements `IDisposable` it's not an excuse to dispose it. – Xriuk May 09 '23 at 07:24
40

In EF Core you no longer can execute "free" raw sql. You are required to define a POCO class and a DbSet for that class. In your case you will need to define Rank:

var ranks = DbContext.Ranks
   .FromSql("SQL_SCRIPT OR STORED_PROCEDURE @p0,@p1,...etc", parameters)
   .AsNoTracking().ToList();

As it will be surely readonly it will be useful to include the .AsNoTracking() call.

EDIT - Breaking change in EF Core 3.0:

DbQuery() is now obsolete, instead DbSet() should be used (again). If you have a keyless entity, i.e. it don't require primary key, you can use HasNoKey() method:

ModelBuilder.Entity<SomeModel>().HasNoKey()

More information can be found here

E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • 4
    So I guess I will also have to extend the `DbContext` to include a new property `DbSet Rank { get; set; }`. What implications will this have now in reference to linq? I.e. Wont we now be able to use a statement like `DBContext.Rank.Where(i => i.key == 1)`, and won't this statement have no implementation in SQL and therefore fail? – David Harlow Feb 25 '16 at 17:25
  • Linq emitted against this set have to be resolved in memory. If you need to emit different WHERE sql clause you have to include them as parameters or build a different script. – E-Bat Feb 25 '16 at 20:28
  • 2
    My DbSet does not have a "FromSql" method. Is this an extension I am missing? – birwin Feb 02 '17 at 19:00
  • 2
    @birwin, you need to import namespace Microsoft.EntityFrameworkCore – E-Bat Feb 02 '17 at 20:30
33

For now, until there is something new from EFCore I would used a command and map it manually

  using (var command = this.DbContext.Database.GetDbConnection().CreateCommand())
  {
      command.CommandText = "SELECT ... WHERE ...> @p1)";
      command.CommandType = CommandType.Text;
      var parameter = new SqlParameter("@p1",...);
      command.Parameters.Add(parameter);

      this.DbContext.Database.OpenConnection();

      using (var result = command.ExecuteReader())
      {
         while (result.Read())
         {
            .... // Map to your entity
         }
      }
  }

Try to SqlParameter to avoid Sql Injection.

 dbData.Product.FromSql("SQL SCRIPT");

FromSql doesn't work with full query. Example if you want to include a WHERE clause it will be ignored.

Some Links:

Executing Raw SQL Queries using Entity Framework Core

Raw SQL Queries

Bohdan Stupak
  • 1,455
  • 8
  • 15
Henry
  • 785
  • 9
  • 17
30

You can use this:

    public static class SqlQueryExtensions
    {
        public static IList<T> SqlQuery<T>(this DbContext db, string sql, params object[] parameters) where T : class
        {
            using (var db2 = new ContextForQueryType<T>(db.Database.GetDbConnection()))
            {
                // share the current database transaction, if one exists
                var transaction = db.Database.CurrentTransaction;
                if (transaction != null)
                    db2.Database.UseTransaction(transaction.GetDbTransaction());
                return db2.Set<T>().FromSqlRaw(sql, parameters).ToList();
            }
        }

        public static IList<T> SqlQuery<T>(this DbContext db, Func<T> ignored, string sql, params object[] parameters) where T : class
            => SqlQuery<T>(db, sql, parameters);

        private class ContextForQueryType<T> : DbContext where T : class
        {
            private readonly DbConnection connection;

            public ContextForQueryType(DbConnection connection)
            {
                this.connection = connection;
            }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(connection, options => options.EnableRetryOnFailure());

                base.OnConfiguring(optionsBuilder);
            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<T>().HasNoKey();
                base.OnModelCreating(modelBuilder);
            }
        }
    }

And the usage:

using (var db = new Db())
{
    var results = db.SqlQuery<ArbitraryType>("select 1 id, 'joe' name");
    //or with an anonymous type like this
    var results2 = db.SqlQuery(() => new { id =1, name=""},"select 1 id, 'joe' name");
}
Jeremy Lakeman
  • 9,515
  • 25
  • 29
ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • 1
    Anonymous query `db.SqlQuery(() => new { id =1, name=""},"select 1 id, 'joe' name");` throws compile error `Type arguments for method SqlQuery cannot inferred from usage`. How to use it with anonymuous type. Also Query does not exist in .NET 5. github link contains fixed code – Andrus Jan 26 '21 at 20:42
  • 1
    I believe the update @ErikEJ is referring to is found here: https://github.com/dotnet/efcore/issues/1862#issuecomment-597022290 Brilliant by the way, exactly what I was looking for, thanks! I tried the suggested answer but as of May of 2021 it still tries to create the model. Don't know why the EF team removed db.Database.SqlQuery() without a comparable replacement is beyond me! – Anthony Griggs May 12 '21 at 13:51
  • 1
    @AnthonyGriggs, I don't think EF Core and .NET Core are ready to use from what I am picking up. The more I dig into it the more there's nearly everything missing! They should've just continued maintaining .NET and EF if they knew they had no way of reproducing the good old .NET. Most functions/methods are missing everywhere with so many excuses to why they're missing! – Morgs Jul 15 '21 at 12:29
  • 1
    Improved the answer to work inside a db transaction, and defined the extension method used for anonymous types in the example usage. – Jeremy Lakeman Sep 22 '21 at 01:17
  • @JeremyLakeman I'm confused, but I don't see any extension method that would support anonymous types? – Ish Thomas Mar 22 '23 at 15:58
  • @IshThomas Weird, I remember testing that. Looks like someone else edited it out. – Jeremy Lakeman Mar 22 '23 at 23:52
25

You can execute raw sql in EF Core - Add this class to your project. This will allow you to execute raw SQL and get the raw results without having to define a POCO and a DBSet. See https://github.com/aspnet/EntityFramework/issues/1862#issuecomment-220787464 for original example.

using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.EntityFrameworkCore
{
    public static class RDFacadeExtensions
    {
        public static RelationalDataReader ExecuteSqlQuery(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
        {
            var concurrencyDetector = databaseFacade.GetService<IConcurrencyDetector>();

            using (concurrencyDetector.EnterCriticalSection())
            {
                var rawSqlCommand = databaseFacade
                    .GetService<IRawSqlCommandBuilder>()
                    .Build(sql, parameters);

                return rawSqlCommand
                    .RelationalCommand
                    .ExecuteReader(
                        databaseFacade.GetService<IRelationalConnection>(),
                        parameterValues: rawSqlCommand.ParameterValues);
            }
        }

        public static async Task<RelationalDataReader> ExecuteSqlQueryAsync(this DatabaseFacade databaseFacade, 
                                                             string sql, 
                                                             CancellationToken cancellationToken = default(CancellationToken),
                                                             params object[] parameters)
        {

            var concurrencyDetector = databaseFacade.GetService<IConcurrencyDetector>();

            using (concurrencyDetector.EnterCriticalSection())
            {
                var rawSqlCommand = databaseFacade
                    .GetService<IRawSqlCommandBuilder>()
                    .Build(sql, parameters);

                return await rawSqlCommand
                    .RelationalCommand
                    .ExecuteReaderAsync(
                        databaseFacade.GetService<IRelationalConnection>(),
                        parameterValues: rawSqlCommand.ParameterValues,
                        cancellationToken: cancellationToken);
            }
        }
    }
}

Here's an example of how to use it:

// Execute a query.
using(var dr = await db.Database.ExecuteSqlQueryAsync("SELECT ID, Credits, LoginDate FROM SamplePlayer WHERE " +
                                                          "Name IN ('Electro', 'Nitro')"))
{
    // Output rows.
    var reader = dr.DbDataReader;
    while (reader.Read())
    {
        Console.Write("{0}\t{1}\t{2} \n", reader[0], reader[1], reader[2]);
    }
}
9

try this: (create extension method)

public static List<T> ExecuteQuery<T>(this dbContext db, string query) where T : class, new()
        {
            using (var command = db.Database.GetDbConnection().CreateCommand())
            {
                command.CommandText = query;
                command.CommandType = CommandType.Text;

                db.Database.OpenConnection();

                using (var reader = command.ExecuteReader())
                {
                    var lst = new List<T>();
                    var lstColumns = new T().GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
                    while (reader.Read())
                    {
                        var newObject = new T();
                        for (var i = 0; i < reader.FieldCount; i++)
                        {
                            var name = reader.GetName(i);
                            PropertyInfo prop = lstColumns.FirstOrDefault(a => a.Name.ToLower().Equals(name.ToLower()));
                            if (prop == null)
                            {
                                continue;
                            }
                            var val = reader.IsDBNull(i) ? null : reader[i];
                            prop.SetValue(newObject, val, null);
                        }
                        lst.Add(newObject);
                    }

                    return lst;
                }
            }
        }

Usage:

var db = new dbContext();
string query = @"select ID , Name from People where ... ";
var lst = db.ExecuteQuery<PeopleView>(query);

my model: (not in DbSet):

public class PeopleView
{
    public int ID { get; set; }
    public string Name { get; set; }
}

tested in .netCore 2.2 and 3.0.

Note: this solution has the slow performance

AminRostami
  • 2,585
  • 3
  • 29
  • 45
8

Add Nuget package - Microsoft.EntityFrameworkCore.Relational

using Microsoft.EntityFrameworkCore;
...
await YourContext.Database.ExecuteSqlCommandAsync("... @p0, @p1", param1, param2 ..)

This will return the row numbers as an int

See - https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.executesqlcommand?view=efcore-3.0

LuvForAirplanes
  • 761
  • 8
  • 23
Mohsin
  • 692
  • 1
  • 7
  • 15
7

In Core 2.1 you can do something like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
       modelBuilder.Query<Ranks>();
}

and then define you SQL Procedure, like:

public async Task<List<Ranks>> GetRanks(string value1, Nullable<decimal> value2)
{
    SqlParameter value1Input = new SqlParameter("@Param1", value1?? (object)DBNull.Value);
    SqlParameter value2Input = new SqlParameter("@Param2", value2?? (object)DBNull.Value);

    List<Ranks> getRanks = await this.Query<Ranks>().FromSql("STORED_PROCEDURE @Param1, @Param2", value1Input, value2Input).ToListAsync();

    return getRanks;
}

This way Ranks model will not be created in your DB.

Now in your controller/action you can call:

List<Ranks> gettingRanks = _DbContext.GetRanks(value1,value2).Result.ToListAsync();

This way you can call Raw SQL Procedures.

RodrigoCampos
  • 172
  • 1
  • 5
  • The `FromSql` params could be simply passed without creating `SqlParameter` object: `FromSql($"STORED_PROCEDURE {value1}, {value2}")` or `FromSql("STORED_PROCEDURE {0}, {1}", value1, value2)` (they will be escaped). – Majid Apr 20 '19 at 19:34
6

I found the package EntityFrameworkCore.RawSQLExtensions on github. To use it, add the nuget package.

<PackageReference Include="EntityFrameworkCore.RawSQLExtensions" Version="1.2.0" />

The library is not documented but below is my using of it with .NET 6 + EF Core 6 + Npgsql 6

public class DbResult
{
    public string Name { get; set; }
    public int Age { get; set; }
}
using EntityFrameworkCore.RawSQLExtensions.Extensions;
var results = await context.Database
    .SqlQuery<DbResult>(
        @"select name, age from ""users"" where age > @Age",
        new NpgsqlParameter("@Age", 15))
    .ToListAsync();
Belyansky Ilya
  • 331
  • 5
  • 4
5

I used Dapper to bypass this constraint of Entity framework Core.

IDbConnection.Query

is working with either sql query or stored procedure with multiple parameters. By the way it's a bit faster (see benchmark tests )

Dapper is easy to learn. It took 15 minutes to write and run stored procedure with parameters. Anyway you may use both EF and Dapper. Below is an example:

 public class PodborsByParametersService
{
    string _connectionString = null;


    public PodborsByParametersService(string connStr)
    {
        this._connectionString = connStr;

    }

    public IList<TyreSearchResult> GetTyres(TyresPodborView pb,bool isPartner,string partnerId ,int pointId)
    {

        string sqltext  "spGetTyresPartnerToClient";

        var p = new DynamicParameters();
        p.Add("@PartnerID", partnerId);
        p.Add("@PartnerPointID", pointId);

        using (IDbConnection db = new SqlConnection(_connectionString))
        {
            return db.Query<TyreSearchResult>(sqltext, p,null,true,null,CommandType.StoredProcedure).ToList();
        }


        }
}
Lapenkov Vladimir
  • 3,066
  • 5
  • 26
  • 37
3

My case used stored procedure instead of raw SQL

Created a class

Public class School
{
    [Key]
    public Guid SchoolId { get; set; }
    public string Name { get; set; }
    public string Branch { get; set; }
    public int NumberOfStudents  { get; set; }
}

Added below on my DbContext class

public DbSet<School> SP_Schools { get; set; }

To execute the stored procedure:

var MySchools = _db.SP_Schools.FromSqlRaw("GetSchools @schoolId, @page, @size ",
              new SqlParameter("schoolId", schoolId),
              new SqlParameter("page", page),
              new SqlParameter("size", size)))
.IgnoreQueryFilters();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NoloMokgosi
  • 1,678
  • 16
  • 10
3

I updated extension method from @AminRostami to return IAsyncEnumerable (so LINQ filtering can be applied) and it's mapping Model Column name of records returned from DB to models (Tested with EF Core 5):

Extension itself:

public static class QueryHelper
{
    private static string GetColumnName(this MemberInfo info)
    {
        List<ColumnAttribute> list = info.GetCustomAttributes<ColumnAttribute>().ToList();
        return list.Count > 0 ? list.Single().Name : info.Name;
    }
    /// <summary>
    /// Executes raw query with parameters and maps returned values to column property names of Model provided.
    /// Not all properties are required to be present in model (if not present - null)
    /// </summary>
    public static async IAsyncEnumerable<T> ExecuteQuery<T>(
        [NotNull] this DbContext db,
        [NotNull] string query,
        [NotNull] params SqlParameter[] parameters)
        where T : class, new()
    {
        await using DbCommand command = db.Database.GetDbConnection().CreateCommand();
        command.CommandText = query;
        command.CommandType = CommandType.Text;
        if (parameters != null)
        {
            foreach (SqlParameter parameter in parameters)
            {
                command.Parameters.Add(parameter);
            }
        }
        await db.Database.OpenConnectionAsync();
        await using DbDataReader reader = await command.ExecuteReaderAsync();
        List<PropertyInfo> lstColumns = new T().GetType()
            .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
        while (await reader.ReadAsync())
        {
            T newObject = new();
            for (int i = 0; i < reader.FieldCount; i++)
            {
                string name = reader.GetName(i);
                PropertyInfo prop = lstColumns.FirstOrDefault(a => a.GetColumnName().Equals(name));
                if (prop == null)
                {
                    continue;
                }
                object val = await reader.IsDBNullAsync(i) ? null : reader[i];
                prop.SetValue(newObject, val, null);
            }
            yield return newObject;
        }
    }
}

Model used (note that Column names are different than actual property names):

public class School
{
    [Key] [Column("SCHOOL_ID")] public int SchoolId { get; set; }

    [Column("CLOSE_DATE", TypeName = "datetime")]
    public DateTime? CloseDate { get; set; }

    [Column("SCHOOL_ACTIVE")] public bool? SchoolActive { get; set; }
}

Actual usage:

public async Task<School> ActivateSchool(int schoolId)
{
    // note that we're intentionally not returning "SCHOOL_ACTIVE" with select statement
    // this might be because of certain IF condition where we return some other data
    return await _context.ExecuteQuery<School>(
        "UPDATE SCHOOL SET SCHOOL_ACTIVE = 1 WHERE SCHOOL_ID = @SchoolId; SELECT SCHOOL_ID, CLOSE_DATE FROM SCHOOL",
        new SqlParameter("@SchoolId", schoolId)
    ).SingleAsync();
}
Jernej Habjan
  • 148
  • 1
  • 14
  • 1
    I have to say that this is the most useful answer. Nevertheless, it is not as simple as it looks and there are many edge case issues (e.g. handling nullable types, transactions, string, DateTime, Guid, decimal types, ...). I have code (based on the code above) which deals with all that and I will post it here when I have some free time. – Konstantin Konstantinov Aug 26 '21 at 00:21
  • This is it! No DbSet required, prevents SQL injection, automatically maps to target class (T). Thanks alot. – thomasgalliker Sep 03 '21 at 11:40
  • @KonstantinKonstantinov would you mind to post your update please? – SeriousM Sep 21 '21 at 15:18
  • @SeriousM - I've just posted the code. – Konstantin Konstantinov Sep 21 '21 at 23:44
3

Done this for Entity Framework Core 5, need to install

Microsoft.EntityFrameworkCore.Relational

The helper extension methods

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;


public static class EfHelper
{
    public static DbTransaction GetDbTransaction(this IDbContextTransaction source)
    {
        return (source as IInfrastructure<DbTransaction>).Instance;
    }

    private class PropertyMapp
    {
        public string Name { get; set; }
        public Type Type { get; set; }

        public bool IsSame(PropertyMapp mapp)
        {
            if (mapp == null)
            {
                return false;
            }
            bool same = mapp.Name == Name && mapp.Type == Type;
            return same;
        }
    }

    public static IEnumerable<T> FromSqlQuery<T>(this DbContext context, string query, params object[] parameters) where T : new()
    {
        const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
        List<PropertyMapp> entityFields = (from PropertyInfo aProp in typeof(T).GetProperties(flags)
                                           select new PropertyMapp
                                           {
                                               Name = aProp.Name,
                                               Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType
                                           }).ToList();
        List<PropertyMapp> dbDataReaderFields = new List<PropertyMapp>();
        List<PropertyMapp> commonFields = null;

        using (var command = context.Database.GetDbConnection().CreateCommand())
        {
            if (command.Connection.State != ConnectionState.Open)
            {
                command.Connection.Open();
            }
            var currentTransaction = context.Database.CurrentTransaction;
            if (currentTransaction != null)
            {
                command.Transaction = currentTransaction.GetDbTransaction();
            }
            command.CommandText = query;
            if (parameters.Any())
            {
                command.Parameters.AddRange(parameters);
            }
            using (var result = command.ExecuteReader())
            {
                while (result.Read())
                {
                    if (commonFields == null)
                    {
                        for (int i = 0; i < result.FieldCount; i++)
                        {
                            dbDataReaderFields.Add(new PropertyMapp { Name = result.GetName(i), Type = result.GetFieldType(i) });
                        }
                        commonFields = entityFields.Where(x => dbDataReaderFields.Any(d => d.IsSame(x))).Select(x => x).ToList();
                    }

                    var entity = new T();
                    foreach (var aField in commonFields)
                    {
                        PropertyInfo propertyInfos = entity.GetType().GetProperty(aField.Name);
                        var value = (result[aField.Name] == DBNull.Value) ? null : result[aField.Name]; //if field is nullable
                        propertyInfos.SetValue(entity, value, null);
                    }
                    yield return entity;
                }
            }
        }
    }

    /*
     * https://entityframeworkcore.com/knowledge-base/35631903/raw-sql-query-without-dbset---entity-framework-core
     */
    public static IEnumerable<T> FromSqlQuery<T>(this DbContext context, string query, Func<DbDataReader, T> map, params object[] parameters)
    {
        using (var command = context.Database.GetDbConnection().CreateCommand())
        {
            if (command.Connection.State != ConnectionState.Open)
            {
                command.Connection.Open();
            }
            var currentTransaction = context.Database.CurrentTransaction;
            if (currentTransaction != null)
            {
                command.Transaction = currentTransaction.GetDbTransaction();
            }
            command.CommandText = query;
            if (parameters.Any())
            {
                command.Parameters.AddRange(parameters);
            }
            using (var result = command.ExecuteReader())
            {
                while (result.Read())
                {
                    yield return map(result);
                }
            }
        }
    }
}

Model

public class UserModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public bool? IsDeleted { get; set; }
}

Manual mapping

List<UserModel> usersInDb = Db.FromSqlQuery
(
    "SELECT Name, Email FROM Users WHERE Name=@paramName",
    x => new UserModel 
    { 
        Name = (string)x[0], 
        Email = (string)x[1] 
    },
    new SqlParameter("@paramName", user.Name)
)
.ToList();

usersInDb = Db.FromSqlQuery
(
    "SELECT Name, Email FROM Users WHERE Name=@paramName",
    x => new UserModel 
    { 
        Name = x["Name"] is DBNull ? "" : (string)x["Name"], 
        Email = x["Email"] is DBNull ? "" : (string)x["Email"] 
    },
    new SqlParameter("@paramName", user.Name)
)
.ToList();

Auto mapping using reflection

List<UserModel> usersInDb = Db.FromSqlQuery<UserModel>
(
    "SELECT Name, Email, IsDeleted FROM Users WHERE Name=@paramName",
    new SqlParameter("@paramName", user.Name)
)
.ToList();
Dipon Roy
  • 396
  • 1
  • 4
  • 18
2

Not directly targeting the OP's scenario, but since I have been struggling with this, I'd like to drop these ex. methods that make it easier to execute raw SQL with the DbContext:

public static class DbContextCommandExtensions
{
  public static async Task<int> ExecuteNonQueryAsync(this DbContext context, string rawSql,
    params object[] parameters)
  {
    var conn = context.Database.GetDbConnection();
    using (var command = conn.CreateCommand())
    {
      command.CommandText = rawSql;
      if (parameters != null)
        foreach (var p in parameters)
          command.Parameters.Add(p);
      await conn.OpenAsync();
      return await command.ExecuteNonQueryAsync();
    }
  }

  public static async Task<T> ExecuteScalarAsync<T>(this DbContext context, string rawSql,
    params object[] parameters)
  {
    var conn = context.Database.GetDbConnection();
    using (var command = conn.CreateCommand())
    {
      command.CommandText = rawSql;
      if (parameters != null)
        foreach (var p in parameters)
          command.Parameters.Add(p);
      await conn.OpenAsync();
      return (T)await command.ExecuteScalarAsync();
    }
  }
}
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
2

This solution leans heavily on the solution from @pius. I wanted to add the option to support query parameters to help mitigate SQL injection and I also wanted to make it an extension off of the DbContext DatabaseFacade for Entity Framework Core to make it a little more integrated.

First create a new class with the extension:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;

namespace EF.Extend
{

    public static class ExecuteSqlExt
    {
        /// <summary>
        /// Execute raw SQL query with query parameters
        /// </summary>
        /// <typeparam name="T">the return type</typeparam>
        /// <param name="db">the database context database, usually _context.Database</param>
        /// <param name="query">the query string</param>
        /// <param name="map">the map to map the result to the object of type T</param>
        /// <param name="queryParameters">the collection of query parameters, if any</param>
        /// <returns></returns>
        public static List<T> ExecuteSqlRawExt<T, P>(this DatabaseFacade db, string query, Func<DbDataReader, T> map, IEnumerable<P> queryParameters = null)
        {
            using (var command = db.GetDbConnection().CreateCommand())
            {
                if((queryParameters?.Any() ?? false))
                    command.Parameters.AddRange(queryParameters.ToArray());

                command.CommandText = query;
                command.CommandType = CommandType.Text;

                db.OpenConnection();

                using (var result = command.ExecuteReader())
                {
                    var entities = new List<T>();

                    while (result.Read())
                    {
                        entities.Add(map(result));
                    }

                    return entities;
                }
            }
                
        }
    }

}

Note in the above that "T" is the type for the return and "P" is the type of your query parameters which will vary based on if you are using MySql, Sql, so on.

Next we will show an example. I'm using the MySql EF Core capability, so we'll see how we can use the generic extension above with this more specific MySql implementation:

//add your using statement for the extension at the top of your Controller
//with all your other using statements
using EF.Extend;

//then your your Controller looks something like this
namespace Car.Api.Controllers
{

    //Define a quick Car class for the custom return type
    //you would want to put this in it's own class file probably
    public class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public string DisplayTitle { get; set; }
    }

    [ApiController]
    public class CarController : ControllerBase
    {
        private readonly ILogger<CarController> _logger;
        //this would be your Entity Framework Core context
        private readonly CarContext _context;

        public CarController(ILogger<CarController> logger, CarContext context)
        {
            _logger = logger;
            _context = context;
        }

        //... more stuff here ...

       /// <summary>
       /// Get car example
       /// </summary>
       [HttpGet]
       public IEnumerable<Car> Get()
       {
           //instantiate three query parameters to pass with the query
           //note the MySqlParameter type is because I'm using MySql
           MySqlParameter p1 = new MySqlParameter
           {
               ParameterName = "id1",
               Value = "25"
           };

           MySqlParameter p2 = new MySqlParameter
           {
               ParameterName = "id2",
               Value = "26"
           };

           MySqlParameter p3 = new MySqlParameter
           {
               ParameterName = "id3",
               Value = "27"
           };

           //add the 3 query parameters to an IEnumerable compatible list object
           List<MySqlParameter> queryParameters = new List<MySqlParameter>() { p1, p2, p3 };

           //note the extension is now easily accessed off the _context.Database object
           //also note for ExecuteSqlRawExt<Car, MySqlParameter>
           //Car is my return type "T"
           //MySqlParameter is the specific DbParameter type MySqlParameter type "P"
           List<Car> result = _context.Database.ExecuteSqlRawExt<Car, MySqlParameter>(
        "SELECT Car.Make, Car.Model, CONCAT_WS('', Car.Make, ' ', Car.Model) As DisplayTitle FROM Car WHERE Car.Id IN(@id1, @id2, @id3)",
        x => new Car { Make = (string)x[0], Model = (string)x[1], DisplayTitle = (string)x[2] }, 
        queryParameters);

           return result;
       }
    }
}

The query would return rows like:
"Ford", "Explorer", "Ford Explorer"
"Tesla", "Model X", "Tesla Model X"

The display title is not defined as a database column, so it wouldn't be part of the EF Car model by default. I like this approach as one of many possible solutions. The other answers on this page reference other ways to address this issue with the [NotMapped] decorator, which depending on your use case could be the more appropriate approach.

Note the code in this example is obviously more verbose than it needs to be, but I thought it made the example clearer.

dan-iel
  • 801
  • 8
  • 4
1

Actually you can create a generic repository and do something like this

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : BaseEntity
{
    private readonly DataContext context;
    private readonly DbSet<TEntity> dbSet;

    public GenericRepository(DataContext context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

   
    public IEnumerable<TEntity> ExecuteCommandQuery(string command)
        => dbSet.FromSqlRaw(command);

}
Denys
  • 115
  • 7
  • 3
    addition: Microsoft.EntityFrameworkCore doesn't contain FromSqlRaw. Is is nessesary to install Microsoft.EntityFrameworkCore.Relational to use this method. – Kate Oct 14 '20 at 15:03
  • 4
    It's not possible with efcore5.0 because the TEntity must be registered first. `"Cannot create a DbSet for 'DatabaseFirewallRuleModel' because this type is not included in the model for the context."` – SeriousM Sep 21 '21 at 14:53
  • wish I had read @SeriousM comment a couple hours ago. Is there a solution to this in EF Core 6 or 7 ? – Perposterer Jan 04 '23 at 05:42
1

For Querying Data: Without existing Entity

               string query = "SELECT r.Name as roleName, ur.roleId, u.Id as userId   FROM dbo.AspNetUserRoles AS ur INNER JOIN dbo.AspNetUsers AS u ON ur.UserId = u.Id INNER JOIN dbo.AspNetRoles AS r ON ur.RoleId = r.Id ";

               ICollection<object> usersWithRoles = new List<object>();
                using (var command = _identityDBContext.Database.GetDbConnection().CreateCommand())
                {
                    command.CommandText = query;
                    command.CommandType = CommandType.Text;

                    await _identityDBContext.Database.OpenConnectionAsync();

                    using (var reader = await command.ExecuteReaderAsync())
                    {      
                        while (await reader.ReadAsync())
                        {
                            usersWithRoles.Add(new { 
                                roleName = reader.GetFieldValueAsync<string>(0).Result, 
                                roleId = reader.GetFieldValueAsync<string>(1).Result,
                                userId = reader.GetFieldValueAsync<string>(2).Result
                            });
                        }    
                    }
                }

Detailed:

 [HttpGet]
    [Route("GetAllUsersWithRoles")]
    public async Task<IActionResult> GetAllUsersWithRoles()
    {
        string query = "SELECT r.Name as roleName, ur.roleId, u.Id as userId   FROM dbo.AspNetUserRoles AS ur INNER JOIN dbo.AspNetUsers AS u ON ur.UserId = u.Id INNER JOIN dbo.AspNetRoles AS r ON ur.RoleId = r.Id ";
        try
        {
            ICollection<object> usersWithRoles = new List<object>();
            using (var command = _identityDBContext.Database.GetDbConnection().CreateCommand())
            {
                command.CommandText = query;
                command.CommandType = CommandType.Text;

                await _identityDBContext.Database.OpenConnectionAsync();

                using (var reader = await command.ExecuteReaderAsync())
                {      
                    while (await reader.ReadAsync())
                    {
                        usersWithRoles.Add(new { 
                            roleName = reader.GetFieldValueAsync<string>(0).Result, 
                            roleId = reader.GetFieldValueAsync<string>(1).Result,
                            userId = reader.GetFieldValueAsync<string>(2).Result
                        });
                    }    
                }
            }
                return StatusCode(200, usersWithRoles); // Get all users   
        }
        catch (Exception e)
        {
            return StatusCode(500, e);
        }
    }

RESULT looks like this:

[
  {
    "roleName": "admin",
    "roleId": "7c9cb1be-e987-4ec1-ae4d-e4c9790f57d8",
    "userId": "12eadc86-6311-4d5e-8be8-df30799df265"
  },
  {
    "roleName": "user",
    "roleId": "a0d5ef46-b1e6-4a53-91ce-9ff5959f1ed8",
    "userId": "12eadc86-6311-4d5e-8be8-df30799df265"
  },
  {
    "roleName": "user",
    "roleId": "a0d5ef46-b1e6-4a53-91ce-9ff5959f1ed8",
    "userId": "3e7cd970-8c52-4dd1-847c-f824671ea15d"
  }
]
Stefan27
  • 845
  • 8
  • 19
0

You can also use QueryFirst. Like Dapper, this is totally outside EF. Unlike Dapper (or EF), you don't need to maintain the POCO, you edit your sql SQL in a real environment, and it's continually revalidated against the DB. Disclaimer: I'm the author of QueryFirst.

bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
0

I've came to this question because we have over 100 instances of entity-less usages of SqlQuery in Entity Framework 6 and so going the Microsoft suggested way(s) simply cannot not easily work in our case.

In addition, we had to maintain a single EF (Entity Framework 6) / EFC (Entity Framework Core 5) code base for several months, while migrating from EF to EFC. The code base is fairly large and it was simply impossible to migrate "overnight".

The answer below is based on great answers above and it is just a small extension to make them work for a few more edge cases.

First, for each EF based project we created an EFC based project (e.g. MyProject.csproj ==> MyProject_EFC.csproj) and inside all such EFC projects we defined a constant EFCORE. If you are doing a quick one-time migration from EF to EFC, then you don't need that and you can just keep what's inside #if EFCORE ... #else and remove what's inside #else ... #endif below.

Here is the main interop extension class.

using System;
using System.Collections.Generic;
using System.Threading;

#if EFCORE
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using Database = Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade;
using MoreLinq.Extensions;
#else
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
#endif

namespace YourNameSpace.EntityFrameworkCore
{
    /// <summary>
    /// Collection of extension methods to simplify migration from EF to EFC.
    /// </summary>
    public static class EntityFrameworkCoreInterop
    {
        /// <summary>
        /// https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property
        /// </summary>
        public static TAttribute? TryGetAttribute<TAttribute>(this PropertyInfo prop) where TAttribute : Attribute =>
            prop.GetCustomAttributes(true).TryGetAttribute<TAttribute>();

        public static TAttribute? TryGetAttribute<TAttribute>(this Type t) where TAttribute : Attribute =>
            t.GetCustomAttributes(true).TryGetAttribute<TAttribute>();

        public static TAttribute? TryGetAttribute<TAttribute>(this IEnumerable<object> attrs) where TAttribute : Attribute
        {
            foreach (object attr in attrs)
            {
                switch (attr)
                {
                    case TAttribute t:
                    {
                        return t;
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Returns true if the source string matches *any* of the passed-in strings (case insensitive)
        /// </summary>
        public static bool EqualsNoCase(this string? s, params string?[]? targets)
        {
            if (s == null && (targets == null || targets.Length == 0))
            {
                return true;
            }

            if (targets == null)
            {
                return false;
            }

            return targets.Any(t => string.Equals(s, t, StringComparison.OrdinalIgnoreCase));
        }

#if EFCORE
        public class EntityException : Exception
        {
            public EntityException(string message) : base(message)
            {
            }
        }

        public static TEntity GetEntity<TEntity>(this EntityEntry<TEntity> entityEntry)
            where TEntity : class => entityEntry.Entity;

        #region SqlQuery Interop

        /// <summary>
        /// kk:20210727 - This is a little bit ugly but given that this interop method is used just once,
        /// it is not worth spending more time on it.
        /// </summary>
        public static List<T> ToList<T>(this IOrderedAsyncEnumerable<T> e) =>
            Task.Run(() => e.ToListAsync().AsTask()).GetAwaiter().GetResult();

        private static string GetColumnName(this MemberInfo info) =>
            info.GetCustomAttributes().TryGetAttribute<ColumnAttribute>()?.Name ?? info.Name;

        /// <summary>
        /// See: https://stackoverflow.com/questions/35631903/raw-sql-query-without-dbset-entity-framework-core
        /// Executes raw query with parameters and maps returned values to column property names of Model provided.
        /// Not all properties are required to be present in the model. If not present then they will be set to nulls.
        /// </summary>
        private static async IAsyncEnumerable<T> ExecuteQuery<T>(this Database database, string query, params object[] parameters)
        {
            await using DbCommand command = database.GetDbConnection().CreateCommand();
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            if (database.CurrentTransaction != null)
            {
                command.Transaction = database.CurrentTransaction.GetDbTransaction();
            }

            foreach (var parameter in parameters)
            {
                // They are supposed to be of SqlParameter type but are passed as objects.
                command.Parameters.Add(parameter);
            }

            await database.OpenConnectionAsync();
            await using DbDataReader reader = await command.ExecuteReaderAsync();
            var t = typeof(T);

            // TODO kk:20210825 - I do know that the code below works as we use it in some other place where it does work.
            // However, I am not 100% sure that R# proposed version does. Check and refactor when time permits.
            //
            // ReSharper disable once CheckForReferenceEqualityInstead.1
            if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                t = Nullable.GetUnderlyingType(t)!;
            }

            var lstColumns = t
                .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                .ToList();

            while (await reader.ReadAsync())
            {
                if (t.IsPrimitive || t == typeof(string) || t == typeof(DateTime) || t == typeof(Guid) || t == typeof(decimal))
                {
                    var val = await reader.IsDBNullAsync(0) ? null : reader[0];
                    yield return (T) val!;
                }
                else
                {
                    var newObject = Activator.CreateInstance<T>();

                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        var name = reader.GetName(i);
                        var val = await reader.IsDBNullAsync(i) ? null : reader[i];
                        var prop = lstColumns.FirstOrDefault(a => a.GetColumnName().EqualsNoCase(name));

                        if (prop == null)
                        {
                            continue;
                        }

                        prop.SetValue(newObject, val, null);
                    }

                    yield return newObject;
                }
            }
        }

        #endregion

        public static DbRawSqlQuery<TElement> SqlQuery<TElement>(this Database database, string sql, params object[] parameters) =>
            new(database, sql, parameters);

        public class DbRawSqlQuery<TElement> : IAsyncEnumerable<TElement>
        {
            private readonly IAsyncEnumerable<TElement> _elements;

            internal DbRawSqlQuery(Database database, string sql, params object[] parameters) =>
                _elements = ExecuteQuery<TElement>(database, sql, parameters);

            public IAsyncEnumerator<TElement> GetAsyncEnumerator(CancellationToken cancellationToken = new ()) =>
                _elements.GetAsyncEnumerator(cancellationToken);

            public async Task<TElement> SingleAsync() => await _elements.SingleAsync();
            public TElement Single() => Task.Run(SingleAsync).GetAwaiter().GetResult();
            public async Task<TElement> FirstAsync() => await _elements.FirstAsync();
            public TElement First() => Task.Run(FirstAsync).GetAwaiter().GetResult();
            public async Task<TElement?> SingleOrDefaultAsync() => await _elements.SingleOrDefaultAsync();
            public async Task<int> CountAsync() => await _elements.CountAsync();
            public async Task<List<TElement>> ToListAsync() => await _elements.ToListAsync();
            public List<TElement> ToList() => Task.Run(ToListAsync).GetAwaiter().GetResult();

        }
#endif
    }
}

and the usages are indistinguishable from the former EF usages:

public async Task<List<int>> GetMyResults()
{
    using var ctx = GetMyDbContext();
    const string sql = "select 1 as Result";
    return await ctx.GetDatabase().SqlQuery<int>(sql).ToListAsync();
}

where GetMyDbContext is a method to get your database context and GetDatabase is an one-liner interop that returns ((DbContext)context).Database for a given IMyDbContext : DbContext. This is to simplify simultaneous EF / EFC operations.

This works for primitive types (the example is above), entities, local classes (but not anonymous ones). Column renaming is supported via GetColumnName, but, ... it was already done above.

-8

With Entity Framework 6 you can execute something like below

Create Modal Class as

Public class User
{
        public int Id { get; set; }
        public string fname { get; set; }
        public string lname { get; set; }
        public string username { get; set; }
}

Execute Raw DQL SQl command as below:

var userList = datacontext.Database.SqlQuery<User>(@"SELECT u.Id ,fname , lname ,username FROM dbo.Users").ToList<User>();
Siddhartha
  • 1,473
  • 14
  • 10