Core可以运行非查询调用吗

Core可以运行非查询调用吗

本文介绍了Entity Framework Core可以运行非查询调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我的EF应用程序必须调用我无法更改的存储过程。虽然这不是理想的,但我通常可以绕开它。但是,我有一个存储过程,它不会返回任何内容。 EF核心如何处理?我知道在以前的版本中您可以运行ExecuteNonQuery,但在EF Core中却找不到类似的东西。

Unfortunately my EF application has to call stored procedures I am unable to change. While this is not ideal I can normally get around it. However, I have a stored proc that does return anything. How does EF core deal with this? I know in previous versions you could run ExecuteNonQuery but I haven't been able to find anything similar in EF Core.

我通常通过这样的助手运行查询,其中T是映射为返回类型EF的类,可以序列化为:

I normally run my queries through a helper as such, where T is a class that maps to a return type EF can serialize to:

context.Set<T>()
.AsNoTracking()
.FromSql(query, args)
.ToListAsync();

但是,看起来Set总是需要一个类型。我从上下文中看不见的其他任何东西都不会允许您拨打不可查询的电话。我丢失了什么吗?

However it looks like Set always requires a type as does .Query. Nothing else I've seen off of context would allow you to make a non-queryable call. Am I missing something?

我正在使用Microsoft.EntityFrameworkCore:1.2.0

I am using Microsoft.EntityFrameworkCore: 1.2.0

推荐答案

您可以使用

using(var context = new SampleContext())
{
    var commandText = "INSERT Categories (CategoryName) VALUES (@CategoryName)";
    var name = new SqlParameter("@CategoryName", "Test");
    context.Database.ExecuteSqlCommand(commandText, name);
}

或者您可以恢复为:

using (var context = new SampleContext())
using (var command = context.Database.GetDbConnection().CreateCommand())
{
    command.CommandText = "DELETE From Table1";
    context.Database.OpenConnection();
    command.ExecuteNonQuery();
}

这篇关于Entity Framework Core可以运行非查询调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:13