String is not nullable by default in Entity Framework Core 6.0
Asked Answered
B

4

7

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.

Burl answered 22/11, 2021 at 15:14 Comment(4)
It's because net6 projects by default have the (stupid) MS thing called NRT turned on by default at project level, and all MS products, including EF Core are pushed to support it, instead of making the real features needed and fixing their bugs. See Working with Nullable Reference Types, but if you ask me, just turn them off at project level while you can.Dotation
@IvanStoev but sir is there any drawback of turning off NRT for project?Burl
For me personally, no (obviously from my previous comment). You just get the behavior before NRTs, and NRTs are nothing more than compiler warnings and do not guarantee one can't get null values at runtime.Dotation
I understand that the warnings in the beginning can be a pain, but after you get used to them and solve them in a correct way, your application will be a lot better. So just be clear, if it can't be null, string to it. Else make ik nullable. Option 1 public string Address { get; set;} = null!; or public string Address? {get;set;} If you add = null! it tells the compiler that is will not be null in the end. the code generated after the migration look like this: [Address] nvarchar(max) NOT NULL,Springtail
R
13

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

enter image description here

Radius answered 23/6, 2022 at 6:23 Comment(2)
Looks like a workaroundEruption
This is very much "fixing" the symptom rather than actually solving the problem.Personification
E
4

You can asign your property as nullable and after migration you will see the changes

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; }
       //add this   ↑
   }
}
Eruption answered 21/7, 2022 at 16:5 Comment(0)
N
1

I could manage this by adding symbol:

#nullable disable

at the very top of the entity class in addition to the same symbol at the top of the DataContext class

Nurse answered 21/3, 2023 at 8:48 Comment(0)
R
1

make it nullable in your Context file by this code:

protected override void OnModelCreating (ModelBuilder modelBuilder)
{
    modelBuilder.Entity<TestModel>().Property(x =>x.Address).HasDefaultValue(null);
}
Reisch answered 18/5, 2023 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.