执行非查询程序不起作用 asp.net 核心

编程入门 行业动态 更新时间:2024-10-23 21:25:27
本文介绍了执行非查询程序不起作用 asp 核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想执行一个存储过程,它返回三个值(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?)

推荐答案

从此链接获取帮助 docs.microsoft/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 核心

本文发布于:2023-11-16 10:11:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   核心   程序   asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!