执行非查询过程不起作用的asp.net核心 [英] Execute non-query procedure not working asp.net core
问题描述
我想执行一个存储过程,该存储过程返回三个值(Email,Name,CompanyID)并获得一个参数(CompanyID),但是它不起作用.
I want to execute a stored procedure which returns three values (Email, Name, CompanyID) and get one parameter (CompanyID) but it's not working.
我创建了一个具有这些属性的类和一个存储过程,该存储过程返回了数据.通过它显示DatabaseFacade
错误.
I have created a class with these properties and a stored procedure which returns the data. By it is showing DatabaseFacade
error.
我的代码是:
List<MyClass> AppUser = new List<MyClass>(); //Class with three properties
SqlParameter param1 = new SqlParameter("@CompanyID", CompanyID);
AppUser = _context.Database.SqlQuery<CC>("GetUserAndRolesForCompany", param1).ToList();
显示此错误:我包含System.Linq
Showing this error: I have include System.Linq
'DatabaseFacade'不包含'SqlQuery'的定义,并且找不到扩展方法'SqlQuery'接受类型为'DatabaseFacade'的第一个参数(您是否缺少using指令或程序集引用?)
'DatabaseFacade' does not contain a definition for 'SqlQuery' and no extension method 'SqlQuery' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?)
推荐答案
从此链接获取帮助在Core 2.0或EntityFramework 7中,不支持ef 6的早期版本中提供的SqlQuery功能.
In Core 2.0 or EntityFramework 7 does not support SqlQuery feature which was available in previous version of ef 6.
下面是在EntityFramework 7中执行sp的示例.
Below is the example how you can execute sp in EntityFramework 7.
public List<UserDetails> GetUserDetailsUsingSP(int LoggedInID)
{
var loggedInUser = new SqlParameter("Id", LoggedInID);
return WidServices
.FromSql("EXECUTE WID_Services_GetAll @Id ", loggedInUser)
.FirstOrDefault();
}
注意:在要在主db上下文类下添加DbSet的位置添加此方法,然后通过实例化dbContext调用此方法.
Note : Add this method where you are adding your DbSet under your main db context class and then call this method by instantiating your dbContext.
这篇关于执行非查询过程不起作用的asp.net核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!