我正在尝试在mac os webapi上使用asp.net 核心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;
}
收到以下错误
错误 CS1061:"DbSet"不包含"FromSql"的定义 并且没有扩展方法"FromSql"接受类型的第一个参数 可以找到"DbSet"(您是否缺少使用指令或 程序集引用?(CS1061(
我正在使用mac os上的实体框架开发webapi。
研究以下链接中的一些查询:- 不带数据库集的原始 SQL 查询 - 实体框架核心
不带数据库集的原始 SQL 查询 - 实体框架核心
https://forums.asp.net/t/1886501.aspx?System+Data+Entity+DbSet+Entities+User+does+not+contain+a+definition+for+FirstOrDefault+
https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationalqueryableextensions.fromsql?view=efcore-2.1
但找不到错误解决方案。谁能让我知道我错过了什么。
安装 NuGet 包Microsoft.EntityFrameworkCore.Relational
然后将 using 语句添加到类中
使用Microsoft.EntityFrameworkCore;
在最新版本中,Microsoft将此方法重命名为:
FromSqlInterpolated()
https://learn.microsoft.com/en-us/ef/core/querying/raw-sql
因此,您必须使用 .从 Sql 到 .FromSql插值。他们在上面的文档中讨论了动机,尽管我不得不说新方法名称的长度是不受欢迎的。
其他答案所说的仍然是必要的 - 您需要安装 Nuget 包Microsoft.EntityFrameworkCore.Relational,并为 Microsoft.EntityFramework 添加 using 语句。
自从乌默尔回答后,这种情况可能已经改变。它现在位于Microsoft.EntityFrameworkCore
命名空间中。但是您需要引用该包。
所以。。。
dotnet add package Microsoft.EntityFrameworkCore.Relational
并添加此行...
using Microsoft.EntityFrameworkCore;
它使用这一行为我解决了这个问题:
using Microsoft.EntityFrameworkCore;
希望这可能有用,因为它与上述问题有关,并且其他人可能会遇到同样的问题。我无法访问 .FromSql 在一个服务中,它驻留在与 Web.Api(MVC 项目(不同的服务项目中,我能够访问 .来自 Web.Api 项目中控制器内的 FromSql。让我感到困惑的是,web.api项目中没有直接引用Microsoft.EntityFrameworkCore.Relation Microsoft.EntityFrameworkCore.SqlServer nuget包被EntityFramework项目引用。通过添加对服务项目的引用,服务现在可以使用 .来自SQLl。这已经是漫长的一天了,但我仍然不知道为什么 web.api 有效,但服务项目需要直接参考。
在Net core 6中,方法名称已更改为FromSqlRaw
。
因此,如果您还没有安装波纹管软件包,则需要安装:
PM > Install-Package Microsoft.EntityFrameworkCore.Relational
并改用FromSqlRaw
:
using Microsoft.Data.SqlClient; //parameters must come from this namespace and not System.Data.SqlClient
var param1Value= new SqlParameter("Param1", "value1");
context.SomeEntity
.FromSqlRaw("EXECUTE dbo.StoredProcedureName @Param1", param1Value)
.ToList();
它使用这一行为我解决了这个问题: $ 使用 System.Linq;
将此行添加到收到此错误的文件顶部。FromSql 是一种扩展方法,在 Microsoft.EntityFrameworkCore.Relational assembly 中实现。
using Microsoft.EntityFrameworkCore.Relational;