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'
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.
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.
Entity Framework Core provides the DbSet. FromSql() method to execute raw SQL queries for the underlying database and get the results as entity objects.
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:
using Microsoft.EntityFrameworkCore;
myContext.Database.ExecuteSqlRaw(@"...sql to excxute...")
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.
You need to add NuGet package reference Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions
to your project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With