执行非查询程序不起作用 asp.net 核心 [英] Execute non-query procedure not working asp.net core

查看:37
本文介绍了执行非查询程序不起作用 asp.net 核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行一个存储过程,它返回三个值(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"的定义,并且找不到接受DatabaseFacade"类型的第一个参数的扩展方法SqlQuery"(您是否缺少 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?)

推荐答案

从此链接获取帮助 https://docs.microsoft.com/en-us/ef/core/querying/raw-sql

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();
        }

注意:在主数据库上下文类下添加 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆