在 Entity Framework Core 6.0 中,字符串默认不可为 null

Saq*_*taq 4 c# ef-code-first entity-framework-core entity-framework-migrations ef-core-6.0

我正在 Entity Framework Core 6.0 中创建一个数据库表。我在我的项目中使用代码优先方法。

TestModelname中有一个字符串类型属性Address

在此输入图像描述

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace TestProjectForCore6.Models
{
   public class TestModel
   {
       [Key]
       [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
       public int Id { get; set; }
       public string Address { get; set; }
   }
}
Run Code Online (Sandbox Code Playgroud)

当我为此模型添加迁移时,它会nullable false在迁移构建器中创建一个列:

在此输入图像描述

在 Entity Framework Core 5.0 中,我们不需要添加显式定义字符串属性为可为空。

小智 13

只需删除 .csproj 文件上的这一行“enable”

在此输入图像描述