简体   繁体   English

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

[英]Default Nullable Parameter in .net6 Entity Framework

I'm trying to learn .NET Identity on a Udemy course.我正在尝试在 Udemy 课程中学习 .NET Identity。 He uses .NET 5 but I'm trying it in .NET 6. He created 2 entities.他使用 .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; }
}

When I tried these, in migrations ImagePath and Gender sections are nullable=false automatically.当我尝试这些时,在迁移中 ImagePath 和 Gender 部分自动为nullable=false

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

**Is this because the difference of SDK's? **这是因为SDK的不同吗? ** **

Should I use?我应该使用吗? to make ImagePath and Gender nullable like below?像下面这样使 ImagePath 和 Gender 可以为空?

public string? ImagePath { get; set; }

public string? Gender { get; set; }

What is the proper way to get rid of this?摆脱这个的正确方法是什么?

Should I remove the migrations and then create db again after add?我应该删除迁移然后在添加后再次创建数据库吗? to properties.到属性。

CS8618 - Non-nullable variable must contain a non-null value when exiting constructor. CS8618 - 不可为 null 的变量在退出构造函数时必须包含一个非 null 值。 Consider declaring it as nullable.考虑将其声明为可为空。

In .NET 5 and earlier, all reference types are nullable and, in EF, you have to specify that the corresponding column is not null using an attribute or the fluent API in the DbContext .在 .NET 5 及更早版本中,所有引用类型都可以为 null,并且在 EF 中,您必须使用属性或 DbContext 中的流式 API 指定相应的列不是DbContext In .NET 6 and later, reference types are non-nullable by default and you specify that they are nullable by appending the ?在 .NET 6 及更高版本中,默认情况下引用类型不可为 null,您可以通过附加? to the type.到类型。 In this context, string?在这种情况下, string? in .NET 6 is equivalent to string in .NET 5. You should make that change and rebuild the database or apply a new migration. in .NET 6 等同于string in .NET 5。您应该进行更改并重建数据库或应用新的迁移。

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

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