Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core 3 raw SQL missing methods

I'm trying to use EF Core 3 to delete all rows from a table like:

db.MyTable.ExecuteSqlRaw("delete from MyTable;");

But I get the error:

DbSet' does not contain a definition for 'ExecuteSqlRaw' and no accessible extension method 'ExecuteSqlRaw' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?)

The Microsoft breaking changes page for EF Core 3 does not offer any advice on if there are special packages required to enable this:

https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#fromsql-executesql-and-executesqlasync-have-been-renamed

These are the Nuget packages I have installed:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.Linq.Expressions" Version="4.3.0" />
<PackageReference Include="System.Linq.Queryable" Version="4.3.0" />

Using statements:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

Note that FromSqlRaw is available, but ExecuteSqlRaw, ExecuteSqlRawAsync , etc are not.

EDIT: I added a using Microsoft.EntityFrameworkCore and the error has changed to:

'DbSet' does not contain a definition for 'ExecuteSqlRaw' and the best extension method overload 'RelationalDatabaseFacadeExtensions.ExecuteSqlRaw(DatabaseFacade, string, params object[])' requires a receiver of type 'DatabaseFacade'

like image 841
FirstDivision Avatar asked Oct 11 '19 19:10

FirstDivision


People also ask

How use raw SQL query in Entity Framework Core?

Entity Framework Core allows you to drop down to raw SQL queries when working with a relational database. Raw SQL queries are useful if the query you want can't be expressed using LINQ. Raw SQL queries are also used if using a LINQ query is resulting in an inefficient SQL query.

What is ExecuteSqlRaw?

ExecuteSqlRaw(DatabaseFacade, String, IEnumerable<Object>) Executes the given SQL against the database and returns the number of rows affected. Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction.

Which of the following executes a raw SQL query in the database using EF core?

Entity Framework Core provides the DbSet. FromSql() method to execute raw SQL queries for the underlying database and get the results as entity objects.


3 Answers

My edit about the new error led me to an answer:

The Microsoft Breaking Changes documentation is just not providing examples for the Execute methods. To get that to work you have to go through the "Database" property instead. So in short, to use those:

  1. Make sure you have using Microsoft.EntityFrameworkCore;
  2. If using an execute use myContext.Database.ExecuteSqlRaw(@"...sql to excxute...")
like image 90
FirstDivision Avatar answered Oct 21 '22 07:10

FirstDivision


I had similar issue with dbContext.Database.ExecuteSqlRaw() and it has been fixed by installing package Microsoft.EntityFrameworkCore.SqlServer without even using the package.

try install-package Microsoft.EntityFrameworkCore.SqlServer in your current project.

like image 30
Bobby Oshaghi Avatar answered Oct 21 '22 05:10

Bobby Oshaghi


You need to add NuGet package reference Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions to your project.

like image 24
CXuesong Avatar answered Oct 21 '22 07:10

CXuesong