0

I'm writing a asp.net mvc3 project with bundle of SQL Server 2012 and EF. At this moment I need to add authorization to it. I Found one project on codplex http://codefirstmembership.codeplex.com/ and added it to my project. Created table in database

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]

and when I try to use CreateUser method it was get me an error, like next:

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.

It's error from the CodeFirstMembershipProvider.cs on the line Context.SaveChanges();

I checked google and it told me that I have differences between model and table in my database, but I don't see any mistakes. Any ideas?

** UPDATE** This is my model class

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; }
}
Rakstit
  • 57
  • 1
  • 13

1 Answers1

2

It means you're trying to put too much information into a field in your table. For example, if you try to create a user with a username 51 characters long:

"123456789012345678901234567890123456789012345678901"

This will throw a String or binary data would be truncated error because the definition of the Username row only allows for 50 characters:

[Username] [nvarchar](50)

You need to find what data you are passing into CreateUser() which is breaking the rules set out by your CREATE TABLE statement.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • I'm pass data to fields Username, Password, Email, LastName, FirstName and IsApproved. String values not grater than 10 symbols and boolean value indicated to false(change to true, the same error) – Rakstit Aug 16 '13 at 12:23
  • In that case you should check your EF model and make sure the lengths in there match the lengths determined by your `CREATE TABLE` statement. If in doubt, delete the entire EF model and start again. – CodingIntrigue Aug 16 '13 at 12:24
  • You're right. Password was be greater than 50. Thank you. And this post help me http://stackoverflow.com/questions/5345890/getting-exact-error-type-in-from-dbvalidationexception/6593432#6593432 too – Rakstit Aug 16 '13 at 12:53