我开始了一个项目与asp.net MVC互联网应用程序…我对默认代码做了一些更改…我创建了自己的用户上下文:
public class UsersDBContext : DbContext
{
public DbSet<UserProfile> UserProfiles { get; set; }
我对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; }
}
我添加了这个列测试。如您所见,该列可为空.....但是在运行时,当我尝试注册一个用户时,我得到这个错误:
不能在列'test'中插入NULL值。列不允许为空。插入失败。
错误发生在这一行:
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
有人知道解决方法吗?
编辑:迁移代码:
public override void Up()
{
AlterColumn("dbo.UserProfile", "test", c => c.Int());
}
public override void Down()
{
AlterColumn("dbo.UserProfile", "test", c => c.Int(nullable: false));
}
在更改数据库后是否更新了实体框架edmx ?我发现有时在进行更改后使用edmx设计器中的更新功能并不能很好地工作。我将从edmx设计器中删除该表,然后重新添加它,然后重新构建您的解决方案。