数据读取器与指定的不兼容。该类型的成员在数据读取器中没有相应的具有相同名称的列

时间:2018-04-02 08:26:53

标签: c# sql-server entity-framework stored-procedures

所以我使用存储过程在使用C#的测试MVC应用程序中向db添加新用户。

我一直收到错误,我认为这是因为我将新添加的用户的ID返回到数据库,注释掉代码什么都没做:(或者说它给了我同样的错误但抱怨另一个变量。

有趣的是,它成功地添加了一个用户,除非它抛出异常。

我仍然希望将ID返回给调用程序,我仍然希望使用参数化查询。

以下代码:

#region AddUser
public static bool AddUser(Models.User user)
{
    bool result = false;
    Models.UserRegistrationPasswordsEntities1 db = new Models.UserRegistrationPasswordsEntities1();
    var queryResult = db.Users.SqlQuery(@"EXECUTE uspAddUser @1, @2, @3, @4, @5, @6",
                                        new SqlParameter("1", user.userFirstName),
                                        new SqlParameter("2", user.userLastName),
                                        new SqlParameter("3", user.userName),
                                        new SqlParameter("4", user.userEmail),
                                        new SqlParameter("5", user.userPassword),
                                        new SqlParameter("6", user.userDateOfBirth)).Single();


    // the ternary opertor is pretty awesome
    result = queryResult == null ? false : true;

    return result;
}
#endregion

下面的Model.user:

public partial class User
{
    public int userID { get; set; }

    [Display(Name = "First Name")]
    [DataType(DataType.Text)]
    [Required(AllowEmptyStrings = false, ErrorMessage ="First name required")]
    public string userFirstName { get; set; }

    [Display(Name = "Last Name")]
    [DataType(DataType.Text)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Last name required")]
    public string userLastName { get; set; }

    [Display(Name = "Username")]
    [DataType(DataType.Text)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Username required")]
    public string userName { get; set; }

    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "email is required")]
    public string userEmail { get; set; }

    [Display(Name = "Date Of Birth")]
    [DataType(DataType.DateTime)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Date of Birth is required")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime userDateOfBirth { get; set;}


    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
    [MinLength(6, ErrorMessage = "Minimum of 6 characters required")]
    public string userPassword { get; set; }

    [Display(Name = "Confirm Password")]
    [DataType(DataType.Password)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Confirm password is required")]
    [Compare("userPassword", ErrorMessage = "Confirm password and password do not match")]
    public string userConfirmPassword { get; set; }

    public bool userStatus { get; set; }
    public bool userEmailVerificationStatus { get; set; }

    public System.Guid userActivationCode { get; set; }
}

以下错误:
这是我收到的错误,存储过程代码注释掉,如下所示。如果我取消注释它会对userFirstName

发出尖叫声
  

数据阅读器与指定的“UserRegistrationPasswordsModel.User”不兼容。类型为“userID”的成员在数据读取器中没有相应的具有相同名称的列。

创建下表:

CREATE TABLE [dbo].[Users] (
    [userID]                      INT              IDENTITY (1, 1) NOT NULL,
    [userFirstName]               VARCHAR (50)     NOT NULL,
    [userLastName]                VARCHAR (50)     NOT NULL,
    [userName]                    VARCHAR (50)     NOT NULL,
    [userEmail]                   VARCHAR (50)     NOT NULL,
    [userPassword]                VARCHAR (255)    NOT NULL,
    [userStatus]                  BIT              DEFAULT ((0)) NOT NULL,
    [userEmailVerificationStatus] BIT              DEFAULT ((0)) NOT NULL,
    [userActivationCode]          UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [userDateOfBirth]             DATETIME         NOT NULL,
PRIMARY KEY CLUSTERED ([userID] ASC)
);

以下存储过程
请注意,底部的SELECT被注释掉,因为我认为可能将ID返回给调用程序是问题所在。在我的最终解决方案中,如果可能的话,我仍然希望以这种方式将Id返回给调用程序。

CREATE PROCEDURE uspAddUser
 @userFirstName                 VARCHAR(50)
,@userLastName                  VARCHAR(50)
,@userName                      VARCHAR(50)
,@userEmail                     VARCHAR(50)
,@userPassword                  VARCHAR(100)
,@userDateOfBirth               DATETIME

AS
SET NOCOUNT ON      -- Report Only Errors
SET XACT_ABORT ON   -- Rollback transaction on error

BEGIN TRANSACTION

DECLARE @userID  INTEGER

-- CREATE new record
INSERT INTO Users(userFirstName, userLastName, userName, userEmail, userPassword, userStatus, userEmailVerificationStatus, userDateOfBirth)
VALUES(@userFirstName, @userLastName, @userName, @userEmail, @userPassword, 0 ,0, @userDateOfBirth)  -- 1 = Active

---- return ID to calling program
--SELECT UserID FROM Users WHERE userFirstName = @userFirstName AND
--                             userLastName = @userLastName   AND
--                             userName = @userName           AND
--                             userEmail = @userEmail

COMMIT TRANSACTION

我在以下搜索解决方案时访问的资源:
Exception : Execute Insert Stored Procedure using Entity framework ExecuteStoreQuery function

https://blogs.msdn.microsoft.com/diego/2012/01/09/stored-procedures-with-output-parameters-using-sqlquery-in-the-dbcontext-api/

Get return value from stored procedure

Using Entity Framework, should I use RETURN or SELECT in my stored procedures?

感谢所有和所有帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

作为一般规则,当使用SELECT语句与SqlQuery方法(而不是单个属性)时,您需要返回full part of the entity。将存储过程内的SELECT语句更改为:

SELECT TOP 1 * FROM Users 
WHERE userFirstName = @userFirstName AND userLastName = @userLastName 
AND userName = @userName AND userEmail = @userEmail

然后,将其执行到queryResult

var queryResult = db.Users.SqlQuery(@"EXECUTE uspAddUser @1, @2, @3, @4, @5, @6",
                                        new SqlParameter("1", user.userFirstName),
                                        new SqlParameter("2", user.userLastName),
                                        new SqlParameter("3", user.userName),
                                        new SqlParameter("4", user.userEmail),
                                        new SqlParameter("5", user.userPassword),
                                        new SqlParameter("6", user.userDateOfBirth)).Single();

如果您希望从userID方法获取AddUser,可以从上面的查询结果中调用它(并将返回类型更改为int而不是bool):

int userId = queryResult.userID;

return userId;

旁注:

由于userID被声明为主键标识列,因此将KeyAttributeDatabaseGeneratedAttribute设置为相应的属性作为EF模型类中的主键:

public partial class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int userID { get; set; }

    // other properties

}

类似问题:

does not have a corresponding column in the data reader with the same name