小编Saq*_*taq的帖子

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

我正在 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 中,我们不需要添加显式定义字符串属性为可为空。

c# ef-code-first entity-framework-core entity-framework-migrations ef-core-6.0

4
推荐指数
1
解决办法
9926
查看次数