Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String is not nullable by default in Entity Framework Core 6.0

I was creating a database table in Entity Framework Core 6.0. I was using code first approach in my project.

There was a string type property in TestModel named Address.

enter image description here

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

When I add migration for this model, it creates a nullable false column in migration builder:

enter image description here

In Entity Framework Core 5.0, we don't need to add explicitly define string property as nullable.

like image 289
Saqlain Mushtaq Avatar asked Jun 08 '26 06:06

Saqlain Mushtaq


1 Answers

Just need to remove this line "enable" on .csproj file

enter image description here

like image 140
Lộc Ngô Thành Avatar answered Jun 10 '26 22:06

Lộc Ngô Thành