1

I was using .NET 5.0 (vs 2019) and would declare my model classes like this with no errors or warnings, but in .NET 6.0 (vs 2022) I get the warning with Name and Chats

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

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

namespace ASPNETCore_Backend.Models
{
    public class Category
    {
        public int Id { get; set; }

        [Required]
        [MaxLength(30)]
        [Column(TypeName = "varchar(30)")]
        public string Name { get; set; }

        // Reference to all the chats that have this category
        public ICollection<Chat> Chats { get; set; }
    }
}

What is the correct way to deal with this, considering this class is a Model for Entity Framework Core.

Thank you

JAmes
  • 261
  • 1
  • 5
  • 15
  • 2
    try add `#nullable enable`, and change property to `public Nullable Name {get;set;}` – Aarnihauta Jan 19 '22 at 04:47
  • 1
    Answered here https://stackoverflow.com/questions/67505347/non-nullable-property-must-contain-a-non-null-value-when-exiting-constructor-co – Mukesh Modhvadiya Jan 19 '22 at 04:54
  • 1
    EF Core NRT vision here [Working with Nullable Reference Types](https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types) – Ivan Stoev Jan 19 '22 at 05:26

0 Answers0