C#C
C#13mo ago
surwren

Puzzled about Nullability in EFCore

"Non-nullable property 'Owner' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable."

I get this warning constantly, and sometimes it causes EFcore to error out when I try to apply migrations.
Say for example I have a
Post
entity that may or may not have an uploaded cover UpCover

    public class Post : BaseEntity
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        public bool SubscriberOnly { get; set; } = false;
        public bool Pinned { get; set; } = false;
        public string Description { get; set; } = "";
        public int Likes { get; set; } = 0;
    
        public float LocLatitude { get; set; } = 0.0f;
        public float LocLongitude { get; set; } = 0.0f;

        // Navigation properties
        public Guid OwnerId { get; set; }
        public User Owner { get; set; }

        public Guid CoverId { get; set; }
        public UpCover Cover { get; set; }

        public ICollection<UpMedia> Media { get; set; }
        public ICollection<CommentThread> CommentThreads { get; set; }
        public ICollection<User> Tagged { get; set; }

        public Post() : base()
        {
            Media = new HashSet<UpMedia>();
            CommentThreads = new HashSet<CommentThread>();
            Tagged = new HashSet<User>();
        }

        public bool HasTaggedLocation()
        {
            return !(LocLatitude == 0.0f && LocLongitude == 0.0f);
        }
    }



How can I account for this on both sides of the relationship?
image.png
Was this page helpful?