类型的第一次机会异常'System.Data.Entity.Core.EntityCommandExecutionException“发生在EntityFramework.SqlServer.dll [英] A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll

查看:1145
本文介绍了类型的第一次机会异常'System.Data.Entity.Core.EntityCommandExecutionException“发生在EntityFramework.SqlServer.dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用实体框架6.我得到的输出消息的错误调用存储过程。


  

类型'System.Data.Entity.Core.EntityCommandExecutionException的第一次机会异常出现在EntityFramework.SqlServer.dll


 使用(VAR上下文=新PartnerPortalEntities2())
 {
    VAR outputParameter =新ObjectParameter(结果,typeof运算(字符串));
    变种US presults = context.assignRoles_CreateAccountAndContacts(user.Id,角色,user.AccountId,user.AccountName,user.ContactId,user.FirstName,user.LastName,outputParameter);
    //控制从来不会出现上面的行之后
    如果(S presults.Equals(1))
    {
       //做一点事
    }
    其他
    {
        // 做一点事
    }    }

当我做了调试,控制转到行的地方之后,我们得到的输出窗口上面的错误和调试器停止调用存储过程,它永远不会进入的if语句。

我已经运行在SQLServer中的存储过程,它工作正常那里。任何想法可能是什么错误。我已经从数据库中生成code内置的环境。


解决方案

作为建议的@Shoe,我在其中捕捉到的异常并表现出精确的错误try catch块已经镶嵌在调用存储过程。

原来是,我是不是合格的确切变量作为输出参数。改变结果的结果作为变量的存储过程是结果定义

  VAR outputParameter =新ObjectParameter(结果,typeof运算(字符串));

I am trying to call a stored procedure using entity framework 6. I get an error on the output message.

A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll

 using (var context = new PartnerPortalEntities2())
 {
    var outputParameter = new ObjectParameter("result", typeof(string));
    var spresults = context.assignRoles_CreateAccountAndContacts(user.Id, role, user.AccountId, user.AccountName, user.ContactId, user.FirstName, user.LastName, outputParameter);
    // Control never comes after the above line
    if(spresults.Equals("1"))
    {
       //Do something
    }
    else
    {
        // Do something
    }

    }

When i do a debug, The control goes to line where the stored procedure is called after which we get the above error on the output window and the debugger stops, it never gets into the if statements.

I have run the stored procedure on SQLserver and it works fine there. Any thoughts what could be the error. I have built the context by generating the code from database.

解决方案

As suggested by @Shoe, i had embeded the call to stored procedure with in a try catch block which caught the exception and showed the exact error.

It turned out to be that i was not passing the exact variable as the output parameter. Changed result to results as the variable defined in stored procedure was results

var outputParameter = new ObjectParameter("result", typeof(string));

这篇关于类型的第一次机会异常'System.Data.Entity.Core.EntityCommandExecutionException“发生在EntityFramework.SqlServer.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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