简体   繁体   English

从EntityFramework执行存储过程-代码优先

[英]Executing a Stored Procedure from EntityFramework - Code First

I have a SQL Stored Procedure (SP) uspGetLocationsforUser , which I am trying from my MVC Project. 我有一个SQL存储过程(SP) uspGetLocationsforUser ,我正在从我的MVC项目中尝试。

I first mapped it into EntityFramework by adding this code to the Context Class (DAL): 我首先通过将以下代码添加到Context Class(DAL)将其映射到EntityFramework:

public virtual ObjectResult<uspGetLocationsforUser_Result> uspGetLocationsforUser(string userName)
{
    var userNameParameter = userName != null ?
        new ObjectParameter("UserName", userName) :
        new ObjectParameter("UserName", typeof(string));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<uspGetLocationsforUser_Result>("uspGetLocationsforUser", userNameParameter);
}

This class was added to the model, to represent the returned result of the SP: 此类已添加到模型中,以表示SP的返回结果:

public class uspGetLocationsforUser_Result
{
    public int LocationID { get; set; }
    public string LocationName { get; set; }
}

I would like to call this from code, and for some reason this didn't work: 我想从代码中调用它,由于某种原因,它不起作用:

I am trying to do this: 我正在尝试这样做:

using (var data = new DAL())
{
List<uspGetLocationsforUser_Result> results = data.uspGetLocationsforUser(userName).ToList(); 
}

Could you please help? 能否请你帮忙?

Thanks in advance. 提前致谢。

Update 更新资料

This is the error message: 这是错误消息:

"The FunctionImport 'uspGetLocationsforUser' could not be found in the container 'DAL'." “在容器'DAL'中找不到FunctionImport'uspGetLocationsforUser'。”

The solution for this was that I had to add the SQL Schema name, under which the Stored Procedure belongs. 解决方案是我必须添加存储过程所属的SQL模式名称。

and then do this: 然后执行以下操作:

var userName = GetUserWindowsLoginName();

SqlParameter logonName = new SqlParameter("@userName", userName);

List<uspGetLocationsforUser_Result> results = data.Database.SqlQuery<uspGetLocationsforUser_Result>("admin.uspGetLocationsforUser @userName", logonName).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架未执行存储过程 - Entityframework not executing Stored Procedure 执行存储过程时EntityFramework超时 - EntityFramework getting timeout when executing stored procedure 在EntityFramework中执行Oracle存储过程 - Executing an Oracle stored procedure within EntityFramework EntityFramework.dll中发生类型&#39;System.NullReferenceException&#39;的异常,但未在用户代码中处理:执行存储过程 - An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code : Executing a stored procedure ORA-01861:从C#代码执行存储过程 - ORA-01861 on executing stored procedure from c# code "<i>Executing stored procedure in Entity Framework - has no key defined.<\/i>在实体框架中执行存储过程 - 没有定义键。<\/b> <i>(Code First)<\/i> (代码优先)<\/b>" - Executing stored procedure in Entity Framework - has no key defined. (Code First) EF 6 Code First From Database - 调用现有存储过程 - EF 6 Code First From Database - Calling existing stored procedure 如何从 EF Core 代码优先方法创建存储过程? - How to create a stored procedure from EF Core code first approach? 首先从Entity Framework 6.1.3调用存储过程 - Call stored procedure from Entity Framework 6.1.3 code-first 从Ef Code First中的存储过程返回自定义表 - Return custom table from Stored Procedure In Ef Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM