所以我使用存储过程在使用C#的测试MVC应用程序中向db添加新用户 .
我一直收到错误,我认为这是因为我将新添加的用户的ID返回到数据库,注释掉代码什么都没做:(或者说它给了我同样的错误但抱怨一个不同的变量 .
有趣的是,它成功地添加了一个用户,除了它抛出异常 .
我仍然希望将ID返回给调用程序,我仍然希望使用参数化查询 .
Code Below:
#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 below:
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; }
}
Error Below:
这是我收到的错误,存储过程代码注释掉,如下所示 . 如果我取消注释它的尖叫声 userFirstName
数据读取器与指定的“UserRegistrationPasswordsModel.User”不兼容 . 类型为“userID”的成员在数据读取器中没有相应的具有相同名称的列 .
Create Table Below:
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)
);
Stored Procedure below:
请注意,底部的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
Resources I accessed searching for solution below:
Exception : Execute Insert Stored Procedure using Entity framework ExecuteStoreQuery function
Get return value from stored procedure
Using Entity Framework, should I use RETURN or SELECT in my stored procedures?
任何和所有的帮助表示赞赏,谢谢 .
1 回答
作为一般规则,当使用
SELECT
语句和SqlQuery
方法(而不是单个属性)时,需要返回full part of the entity . 将存储过程内的SELECT
语句更改为:然后,将其执行到
queryResult
:如果您希望从
AddUser
方法获取userID
,可以从上面的查询结果中调用它(并将返回类型更改为int
而不是bool
):Side note:
自
userID
声明为主键标识列以来,将KeyAttribute
和DatabaseGeneratedAttribute
设置为在EF模型类中将相应属性设置为主键:类似的问题:
does not have a corresponding column in the data reader with the same name