You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EF Core调用ExecuteSqlCommand报错:DatabaseFacade无该方法定义

问题:EF Core执行原生SQL删除语句报错

我想在EF Core中执行原生SQL删除语句,参考了EF6的官方文档编写代码,但运行时出现错误。

我的代码

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

错误提示

DatabaseFacade does not contain a definition for 'ExecuteSqlCommand'

我的accountingContext类代码

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)
        {
        }
    }
}

.csproj文件内容

<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>

请问我哪里操作出错了?


解答

问题出在混淆了EF6和EF Core的API:

  • EF6中执行原生SQL命令用ExecuteSqlCommand,但EF Core(包括你使用的.NET 6对应的EF Core 6)已弃用该方法,替代方案是以下两个方法:
  1. 同步执行:使用ExecuteSqlRaw方法

    using (accountingContext db = new())
    {
        db.Database.ExecuteSqlRaw("DELETE FROM ...");
    }
    
  2. 异步执行(推荐使用,提升系统性能):使用ExecuteSqlRawAsync方法

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

额外注意:

  • 如果SQL语句包含参数,必须用参数化查询避免SQL注入,示例如下:
    db.Database.ExecuteSqlRaw("DELETE FROM TransactionsStaging WHERE Id = {0}", stagingId);
    
    或使用命名参数:
    db.Database.ExecuteSqlRaw("DELETE FROM TransactionsStaging WHERE Id = @Id", new SqlParameter("@Id", stagingId));
    

内容的提问来源于stack exchange,提问作者Musaffar Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:25:38