会员资格ASP.NET MVC中的授权

时间:2013-08-16 12:11:55

标签: asp.net asp.net-mvc asp.net-mvc-3 entity-framework forms-authentication

我正在用SQL Server 2012和EF捆绑编写一个asp.net mvc3项目。此时我需要添加授权。我找到了一个关于codplex http://codefirstmembership.codeplex.com/的项目并将其添加到我的项目中。在数据库中创建表

CREATE TABLE [dbo].[Users](
[UserId] [uniqueidentifier] NOT NULL,
[Username] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Comment] [nvarchar](50) NULL,
[IsApproved] [bit] NULL,
[PasswordFailuresSinceLastSuccess] [int] NULL,
[LastPasswordFailureDate] [datetime] NULL,
[LastActivityDate] [datetime] NULL,
[LastLockoutDate] [datetime] NULL,
[LastLoginDate] [datetime] NULL,
[ConfirmationToken] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[IsLockedOut] [bit] NULL,
[LastPasswordChangedDate] [datetime] NULL,
[PasswordVerificationToken] [nvarchar](50) NULL,
[PasswordVerificationTokenExpirationDate] [datetime] NULL
) ON [PRIMARY]

当我尝试使用CreateUser方法时,它给我一个错误,就像下一个:

String or binary data would be truncated. The statement has been
terminated.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: String or
binary data would be truncated. The statement has been terminated.

CodeFirstCmbersChanges()上的CodeFirstMembershipProvider.cs出现错误;

我检查了谷歌,它告诉我,我的数据库中的模型和表格之间存在差异,但我没有看到任何错误。有什么想法吗?

**更新** 这是我的模型类

public class User {
    [Key]
    public virtual Guid UserId { get; set; }

    [Required]
    public virtual String Username { get; set; }

    [Required]
    public virtual String Email { get; set; }

    [Required, DataType(DataType.Password)]
    public virtual String Password { get; set; }

    public virtual String FirstName { get; set; }
    public virtual String LastName { get; set; }

    [DataType(DataType.MultilineText)]
    public virtual String Comment { get; set; }

    public virtual Boolean IsApproved { get; set; }
    public virtual int PasswordFailuresSinceLastSuccess { get; set; }
    public virtual DateTime? LastPasswordFailureDate { get; set; }
    public virtual DateTime? LastActivityDate { get; set; }
    public virtual DateTime? LastLockoutDate { get; set; }
    public virtual DateTime? LastLoginDate { get; set; }
    public virtual String ConfirmationToken { get; set; }
    public virtual DateTime? CreateDate { get; set; }
    public virtual Boolean IsLockedOut { get; set; }
    public virtual DateTime? LastPasswordChangedDate { get; set; }
    public virtual String PasswordVerificationToken { get; set; }
    public virtual DateTime? PasswordVerificationTokenExpirationDate { get; set; }

    public virtual ICollection<Role> Roles { get; set; }
}

1 个答案:

答案 0 :(得分:2)

这意味着您正在尝试将过多信息放入表格中的字段中。例如,如果您尝试使用长度为51个字符的用户名创建用户:

"123456789012345678901234567890123456789012345678901"

这会抛出String or binary data would be truncated错误,因为Username行的定义只允许50个字符:

[Username] [nvarchar](50)

您需要查找传入CreateUser()的数据,这违反了CREATE TABLE声明规定的规则。