Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use C# 8.0 Nullable Reference Types with Entity Framework Core models?

I am enabling C# 8.0 Nullable Reference Types on a .NET Core 3.0 project. The project uses Entity Framework Core 3.0 to access database.

The following is a data model whose Title should not be null.

public class Vehicle {     public int Id { get; private set; }       public string Title { get; private set; }      // Entity Framework Core is instructed to bind to the private _drivers field in a configuration builder     private readonly List<Driver> _drivers = new List<Driver>();     public IReadOnlyCollection<Driver> Drivers => _drivers.AsReadOnly();      private Vehicle()      {     }      public Vehicle(string title)      {         this.Title = title;     }      public void AddDriver(string name)     {          this._drivers.Add(new Driver(name));     }  }  // A foreign column is defined in a configuration builder public class Driver {     public int Id { get; private set; }       public string Name { get; private set; }      private Driver()      {     }      public Driver(string name)      {         this.Name = name;     }  } 

Own code is supposed to use the public constructors only while the private constructors are there just to allow Entity Framework Core and (potentially also) serialization to bind values from database to these classes/models. The public constructor might have different structure, list and types of arguments than which properties the model has (for example, it might also contains arguments for the first required child, it might have some arguments optional etc.).

However, the compiler generates CS8618 Non-nullable field is uninitialized. Consider declaring as nullable. on the private constructors.

I am able to disable CS8616 for the private constructors by #pragma warning disable CS8618 but I do not consider that as a good idea.

How it is supposed to use C# 8.0 Nullable Reference Types in this scenario? Or is my model bogus or violates best practices - how to do it properly?

Unfortunatelly, I have found not relevant docs or guidance.

like image 369
alik Avatar asked Sep 30 '19 16:09

alik


People also ask

How is C used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

How do I use C on my computer?

It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly. C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute).


1 Answers

There is no proper way to handle non-nullable navigational properties.

  1. Documentation suggests two ways and both are not type safe. Use a backing field and throw InvalidOperationException. It is unclear how it differs from doing nothing and have a NullReferenceException
  2. Suppress it with null forgiving operator

Official documentation link: https://docs.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#non-nullable-properties-and-initialization

like image 100
Mikhail Zhuravlev Avatar answered Sep 18 '22 23:09

Mikhail Zhuravlev