在 Entity Framework Core 2.0 中执行 SQL 命令删除表中的所有数据 [英] Execute SQL command in Entity Framework Core 2.0 to delete all data in a table
问题描述
我想从 Entity Framework Core 2.0 执行 SQL 命令,但我不知道如何执行.
1.- 我需要这样做的原因是我想从数据库表中删除所有数据,并且使用 Context.remove
或 Context.removeRange
会对 DB 产生多次调用(表中的每个数据调用一次).
2.- 我已经读到有一个方法 .ExecuteSqlCommand
可以实现这一点,但是我的 Context.Database 中没有该方法(也许在 Core 2.0 中它被删除了?).这是信息的来源:在实体框架核心和 UWP 中删除表
所以,基本上我需要使用 EF Core 2.0 从代码中删除一个表,据我所知,我需要为此执行 SQL 命令.
谢谢.
这是我的 .csproj,以防万一我遗漏了什么
<属性组><TargetFramework>netcoreapp2.0</TargetFramework><AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback></PropertyGroup><项目组><PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"/><PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All"/><PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All"/></项目组><项目组><!--<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1"/>--><DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0"/><DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0"/></项目组></项目>
确保您引用 Microsoft.EntityFrameworkCore
以包含允许您执行原始 SQL 命令的所有必要扩展方法.>
从源代码库中我找到了 ExecuteSqlCommand
和相关的扩展方法
int count = await context.Database.ExecuteSqlCommandAsync("DELETE FROM [Blogs]");
找到一篇建议使用 ADO.Net 的文章.
首先从上下文中获取一个连接,创建一个命令并执行该命令.
using (var connection = context.Database.GetDbConnection()) {等待 connection.OpenAsync();使用 (var 命令 = connection.CreateCommand()) {command.CommandText = "从 [博客] 中删除";var result = await command.ExecuteNonQueryAsync();}}
I want to execute an SQL command from Entity Framework Core 2.0, but I can't figure out how to do so.
1.- The reason why I need to, is that I want to delete all data from a database table, and using Context.remove
or Context.removeRange
would produce many calls to DB (one for each data in the table).
2.- I've read that there is a method .ExecuteSqlCommand
to accomplish that, but that method is not present in my Context.Database (maybe in Core 2.0 it was removed?). Here is the source of the info: Dropping table In Entity Framework Core and UWP
So, basically I need to delete a table from code using EF Core 2.0 and, as far as I know, I need to execute a SQL command for that.
Thank you.
Here is my .csproj, just in case i'm missing something
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<!--<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" /> -->
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
Ensure that you reference Microsoft.EntityFrameworkCore
to include all the necessary extension methods that would allow you to execute raw SQL commands.
From the source repository I found ExecuteSqlCommand
and related extension methods
int count = await context.Database.ExecuteSqlCommandAsync("DELETE FROM [Blogs]");
Found an article that suggested using ADO.Net.
First you grab a connection from the context, create a command and execute that.
using (var connection = context.Database.GetDbConnection()) {
await connection.OpenAsync();
using (var command = connection.CreateCommand()) {
command.CommandText = "DELETE FROM [Blogs]";
var result = await command.ExecuteNonQueryAsync();
}
}
这篇关于在 Entity Framework Core 2.0 中执行 SQL 命令删除表中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!