简体   繁体   English

EntityFramework.dll中发生类型'System.NullReferenceException'的异常,但未在用户代码中处理:执行存储过程

[英]An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code : Executing a stored procedure

I have my code below: 我的代码如下:

 User user = new User();
  WFMModel db = new WFMModel();
  SqlParameter param1 = new SqlParameter("@username", username);
  SqlParameter param2 = new SqlParameter("@password", password);
  SqlParameter param3 = new SqlParameter("@Version", Version);`



var userData = db.Database.SqlQuery<User>("usp_Rep_App_DotNet_Validate_User_new @username,@password,@Version",
param1, param2, param3);

And I have my User class as below: 我的User类如下:

public class User
    {
        public string UserID;
        public string FirstName;
        public string LastName;
        public string Message;
        public string Department;
    }

But when the code executes, I get the error: An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code. 但是执行代码时,出现错误:EntityFramework.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理。

When I run the stored procedure, I get the data without any issues (no nulls as well). 当我运行存储过程时,我得到的数据没有任何问题(也没有null)。

Any idea what's going on? 知道发生了什么吗? Thanks in advance. 提前致谢。

One of the variables that you are passing into your parameters (either username, password, or Version) is likely null. 您要传递给参数的变量之一(用户名,密码或版本)可能为null。 Set a breakpoint, and then hover your mouse over each variable to see which one is throwing the exception. 设置一个断点,然后将鼠标悬停在每个变量上,以查看哪个变量引发了异常。 You need to take appropriate measures to check these values for null before using them in your query. 您需要采取适当的措施来检查这些值是否为空,然后才能在查询中使用它们。

If you wish for the parameter to be "null" from SQL Server's perspective, you would need to use DBNull.Value - databases use DBNull, C# uses null, and they are not interchangeable (although you might expect that they would be.) 如果希望从SQL Server的角度来看该参数为“ null”,则需要使用DBNull.Value-数据库使用DBNull,C#使用null,并且它们不可互换(尽管您可能希望它们可以互换。)

Are you sure that your UserID column will return string and not an integer? 您确定您的UserID列将返回字符串而不是整数吗?

Also probably one of your parameter values is null. 同样,您的参数值之一可能为null。

I myself ran into a lot of troubles when trying to retreive the return of a SQL stored proc with Entity, theres is about 3 way to do so correctly, here's the links : 当我尝试使用Entity检索SQL存储的proc的返回时,我自己遇到了很多麻烦,大约有3种方法可以正确地做到这一点,这是链接:

This methode worked for me : 这个方法对我有用:

Sql Stored proc and Entity framework 6 SQL存储过程和实体框架6

Many people rather like : 许多人喜欢:

using stored procedure in entity framework 在实体框架中使用存储过程

And here is the long but normal way, that most people use : 这是大多数人使用的漫长而正常的方式:

Getting data from stored procedure with Entity Framework 使用Entity Framework从存储过程获取数据

Surely you will be able to use one of these 3 ways has they all do the same thing : retreive the return of the stored proc. 当然,您将能够使用这三种方式之一来使它们都做同样的事情:检索存储过程的返回。

暂无
暂无

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

相关问题 EntityFramework.dll中出现“System.Xml.XmlException”类型的异常,但未在用户代码中处理 - An exception of type 'System.Xml.XmlException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型&#39;System.Data.SqlClient.SqlException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型&#39;System.InvalidOperationException&#39;的异常,但未在用户代码C#中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code, C# EntityFramework.dll中发生类型为&#39;System.Data.Entity.Core.MappingException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.Data.Entity.Core.MappingException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但在注册时未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code while Registering myproject.DLL中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理 - An exception of type 'System.NullReferenceException' occurred in myproject.DLL but was not handled in user code System.InvalidOperationException &gt;&gt;发生在EntityFramework.dll中,但未在用户代码中处理? - System.InvalidOperationException >> occurred in EntityFramework.dll but was not handled in user code? ASP.NET WebAPI错误:EntityFramework.dll中发生类型为&#39;System.Data.SqlClient.SqlException&#39;的异常,但未在用户代码中处理 - ASP.NET WebAPI error: an exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code UWP-Weather-App.exe中发生类型为&#39;System.NullReferenceException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.NullReferenceException' occurred in UWP-Weather-App.exe but was not handled in user code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM