0

我正在编写一个包含 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.

这是 Context.SaveChanges(); 上 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; }
}
4

1 回答 1

2

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

"123456789012345678901234567890123456789012345678901"

这将引发String or binary data would be truncated错误,因为用户名行的定义只允许 50 个字符:

[Username] [nvarchar](50)

您需要找出您传递的哪些数据CreateUser()违反了您的CREATE TABLE声明所规定的规则。

于 2013-08-16T12:15:46.630 回答