.net实体框架中的默认空参数



我想在Udemy课程上学习。net Identity。他使用。net 5,但我正在尝试。net 6。他创建了2个实体

public class AppRole : IdentityRole<int>
{
public DateTime CreatedTime { get; set; }
}
public class AppUser : IdentityUser<int>
{
public string ImagePath { get; set; }
public string Gender { get; set; }
}

当我尝试这些时,在迁移中ImagePath和Gender部分nullable=false自动.

ImagePath = table.Column<string>(type: "nvarchar(max)", **nullable: false**),
Gender = table.Column<string>(type: "nvarchar(max)",** nullable: false**),

**这是因为SDK的不同吗?* *

我应该使用吗?让ImagePath和Gender像下面这样为空?

public string? ImagePath { get; set; }
public string? Gender { get; set; }

解决这个问题的正确方法是什么?

我应该删除迁移,然后在添加后再次创建db吗?属性。

CS8618 -非空变量在退出构造函数时必须包含一个非空值。考虑将其声明为可空。

在。net 5及更早的版本中,所有引用类型都是可空的,在EF中,您必须使用DbContext中的属性或流畅API指定相应的列不为空。在。net 6及以后的版本中,默认情况下引用类型是不可空的,您可以通过在类型后面附加?来指定它们是可空的。在这种情况下,。net 6中的string?相当于。net 5中的string。您应该进行更改并重新构建数据库或应用新的迁移。

最新更新