错误 CS1061:“DbSet<T>”不包含“FromSql”的定义,并且没有接受“DbSet<T>”类型的第一个参数的扩展方法“FromSql”

San*_*isy 12 c# asp.net asp.net-mvc entity-framework asp.net-core

我正在尝试在 mac os webapi 上使用asp.net core 2.1调用视图或存储过程。

using System;
using System.Linq;
using Auth.Database;
using Microsoft.EntityFrameworkCore;

public virtual IQueryable<T> ExecuteStoreProcView(string viewProcName)
{
    IQueryable<T> queryResult = _entities.Set<T>().FromSql(viewProcName).AsQueryable();
    return queryResult;
}
Run Code Online (Sandbox Code Playgroud)

得到以下错误

错误 CS1061:“DbSet”不包含“FromSql”的定义,并且找不到接受“DbSet”类型的第一个参数的扩展方法“FromSql”(您是否缺少 using 指令或程序集引用?)(CS1061)

我正在 mac os 上使用实体框架开发 webapi。

研究以下链接中的一些查询:- 没有 DbSet 的原始 SQL 查询 - Entity Framework Core

没有 DbSet 的原始 SQL 查询 - Entity Framework Core

https://forums.asp.net/t/1886501.aspx?System+Data+Entity+DbSet+Entities+User+does+not+contain+a+definition+for+FirstOrDefault+

https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationalqueryableextensions.fromsql?view=efcore-2.1

但无法找到错误解决方案。任何人都可以请让我知道我缺少什么。

Chr*_*ini 11

在最新版本中,微软将该方法重命名为:

FromSqlInterpolated()
Run Code Online (Sandbox Code Playgroud)

https://learn.microsoft.com/en-us/ef/core/querying/raw-sql

因此,您必须使用 .FromSql 查找/替换所有代码到 .FromSqlInterpolated。他们在上面的文档中讨论了动机,尽管我不得不说新方法名称的长度是不受欢迎的。

其他答案所说的仍然是必要的 - 您需要安装 Nuget 包 Microsoft.EntityFrameworkCore.Relational,并且为 Microsoft.EntityFramework 添加了 using 语句。


A.R*_*R.F 8

它使用这一行为我解决了这个问题:

using Microsoft.EntityFrameworkCore;
Run Code Online (Sandbox Code Playgroud)


小智 7

安装Microsoft.EntityFrameworkCore.Relationalnuget 包,然后将 using 语句添加到类中

使用 Microsoft.EntityFrameworkCore;


cod*_*ros 5

自从 Umer 回答后,这可能已经改变了。它现在位于Microsoft.EntityFrameworkCore命名空间中。但是你需要引用这个包。

所以...

dotnet add package Microsoft.EntityFrameworkCore.Relational
Run Code Online (Sandbox Code Playgroud)

并添加这一行...

using Microsoft.EntityFrameworkCore;
Run Code Online (Sandbox Code Playgroud)


小智 -2

将此行添加到出现此错误的文件顶部。FromSql 是一种扩展方法,在 Microsoft.EntityFrameworkCore.Relational 程序集中实现。

using Microsoft.EntityFrameworkCore.Relational;
Run Code Online (Sandbox Code Playgroud)