繁体   English   中英

“无法将值NULL插入列”,但我的列为Nullable…Entity Framework 5 asp.net MVC

[英]“Cannot insert the value NULL into column ” but my column is Nullable… Entity Framework 5 asp.net MVC

我使用asp.net MVC Internet应用程序启动了一个项目...对默认代码进行了一些更改...创建了自己的用户上下文:

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; }

}

我添加了此列测试。 如您所见,此列为空。....但是在运行时,尝试注册用户时出现此错误:

无法将值NULL插入列“ test”。列不允许为空。 INSERT失败。

该行中发生错误:

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设计器中删除该表,然后重新添加它,然后重新构建您的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM