gpt4 book ai didi

c# - "Cannot insert the value NULL into column "但我的专栏是 Nullable ... Entity Framework 5 asp.net MVC

转载 作者:太空宇宙 更新时间:2023-11-03 16:15:20 25 4
gpt4 key购买 nike

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

}

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

无法将 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));
}

最佳答案

在对数据库进行更改后,您是否更新了 Entity Framework edmx?我发现有时在进行更改后使用 edmx 设计器中的更新功能效果不佳。我会从 edmx 设计器中删除该表,然后重新添加它,然后重建您的解决方案。

关于c# - "Cannot insert the value NULL into column "但我的专栏是 Nullable ... Entity Framework 5 asp.net MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15961585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com