Musaffar Patel
Musaffar Patel

Reputation: 1372

DatabaseFacade does not contain a definition for 'ExecuteSqlCommand'

I wish to run a raw SQL Delete Query on EF Core and I am suing the following guide to help me along: https://learn.microsoft.com/en-us/ef/ef6/querying/raw-sql

The code:

using (accountingContext db = new())
{
    db.Database.ExecuteSqlCommand("DELETE FROM ...");
}

but I get the error:

DatabaseFacade does not contain a definition for 'ExecuteSqlCommand'

my accountingContext class:

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

namespace WebApi.Models
{
    public partial class accountingContext : DbContext
    {
        public DbSet<User>? Users { get; set; }
        public DbSet<Transaction>? Transactions { get; set; }
        public DbSet<TransactionStaging>? TransactionsStaging { get; set; }

        public accountingContext()
        {
        }

        public accountingContext(DbContextOptions<accountingContext> options) : base(options)
        {
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("****");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        }
    }
}

the .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="CsvHelper" Version="28.0.1" />
        <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.9">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.6" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.6">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
        <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.15.0" />
    </ItemGroup>
    <ItemGroup>
      <Folder Include="Resources\Temp\" />
    </ItemGroup>
</Project>

What am I doing wrong here?

Upvotes: 8

Views: 13998

Answers (3)

Jonathan Ramos
Jonathan Ramos

Reputation: 2161

For EF Cor you need to use the ExecuteSqlRaw or ExecuteSql commands included in the Extensions Nuget package Microsoft.EntityFrameworkCore.Relational

Upvotes: 1

Musaffar Patel
Musaffar Patel

Reputation: 1372

The following resolved the issue for me:

db.Database.ExecuteSqlRaw("...");

Not sure why all the docs I was going through were suggesting the use of ExecuteSqlCommand but eventually I stumbled on the correct command.

Upvotes: 15

Paul Sinnema
Paul Sinnema

Reputation: 2792

You need to add the following packages to be able to use the EF 6.0:

enter image description here

Don't use any other EntityFramework packages. Also, your solution should be for .Net 6.0.

Upvotes: 1

Related Questions