2

I started a project with asp.net MVC internet aplication... I made some changes to the default code... I created my own user context:

public class UsersDBContext : DbContext
{

    public DbSet<UserProfile> UserProfiles { get; set; }

And I made some changes to the AccountModel:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public int? test { get; set; }

}

I added this column test. As you can see , this column is nullable..... But in runtime I get this error when I try to register an user:

Cannot insert the value NULL into column 'test'.column does not allow nulls. INSERT fails.

The error happens in this line:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

Does anyone know any solution?

EDIT: Migration Code:

    public override void Up()
    {
        AlterColumn("dbo.UserProfile", "test", c => c.Int());
    }

public override void Down()
    {
        AlterColumn("dbo.UserProfile", "test", c => c.Int(nullable: false));
    }
Ze Carioca Silva
  • 445
  • 7
  • 16
  • 1
    It doesn't matter what data type you use in your entity.. if the database column itself is not nullable it will fail. – Simon Whitehead Apr 12 '13 at 01:26
  • I already updated the dataBase... – Ze Carioca Silva Apr 12 '13 at 01:27
  • have a look here http://stackoverflow.com/questions/4444407/entity-framework-ctp-4-cannot-insert-the-value-null-into-column-even-though?rq=1 – Prabhu Murthy Apr 12 '13 at 01:30
  • 1
    @ZeCariocaSilva, it doesn't sound like you update to the db took. Can you show us your db in SSMS (or the result of a describe) to confirm that it is in fact updated to allow nulls on that column? – Kirk Woll Apr 12 '13 at 01:47

1 Answers1

0

Did you update your entity framework edmx after you made the change to the DB? I have found sometimes that using the update function in the edmx designer after making a change doesn't work very well. I would delete the table from the edmx designer and then re-add it and then rebuild your solution.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116