C#C
C#10mo ago
Faker

✅ ? operator and property assigned to =null! meaning in EF core database context

C#

using System;
using System.Collections.Generic;

namespace Learning.Models;

public partial class Book
{
    public int BookId { get; set; }

    public string Title { get; set; } = null!;

    public string? Author { get; set; }

    public string? Isbn { get; set; }

    public int? PublishedYear { get; set; }
    
    public DateTime Date { get; set; }
    public string BookVersion { get; set; }

    public virtual ICollection<Rental> Rentals { get; set; } = new List<Rental>();

    public override string ToString()
    {
        return $"Title: {Title}, Author: {Author}, ISBN: {Isbn}";
    }
}


Hello guys, this is a book class that I scaffolded using EF Core. I have a few questions. What does that mean when we use = null! and what does that mean when we say a type of associated with ?. What does it convey in terms of database context, like nullable/ not nullable?

I know that ? is used to say that we allow a data type to have null value. But what does it mean here?

Also, =null basically means we are assigning null as soon as we are creating, but what does the ! mean then?

Whys is the =null! important please, can we omit it?
Was this page helpful?