Share via

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

mehmood tekfirst 771 Reputation points
Apr 28, 2022, 6:57 AM

Hi,

I am developing an api in asp.net webapi core 6.
Following is my datacontext. My class seems to be 100% compatible as I see in the Microsoft/MSDN documentations but why I am getting the warnings in Visual Studio 2022 ?

(https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-6.0)

 public class CarRentalContext : DbContext  
    {  
        public CarRentalContext(DbContextOptions<CarRentalContext> options)  
           : base(options)  
        {  
        }  
  
        public DbSet<FranchiseWebInquiry> FranchiseWebInquiries { get; set; }  
        public virtual DbSet<Franchise> Franchises { get; set; }  
        public virtual DbSet<Franchise_BankHolidays> Franchise_BankHolidays { get; set; }  
        public virtual DbSet<Franchise_OpeningHours> FranchiseOpeningHours { get; set; }  
        public virtual DbSet<Enquiry> Enquiries { get; set; }  
        public virtual DbSet<Fleet> Fleets { get; set; }  
        public virtual DbSet<Tariff> Tariffs { get; set; }  
        public virtual DbSet<BookingAgreement> BookingAgreements { get; set; }  
        
    }  

See my warning messages.
Why I would have to create a property as nullable in datacontent while using EF CORE 6.

Severity Code Description Project File Line Suppression State
Warning CS8618 Non-nullable property 'FranchiseWebInquiries' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\Temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active
Warning CS8618 Non-nullable property 'Franchises' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active
Warning CS8618 Non-nullable property 'Franchise_BankHolidays' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active
Warning CS8618 Non-nullable property 'FranchiseOpeningHours' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active
Warning CS8618 Non-nullable property 'Enquiries' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active
Warning CS8618 Non-nullable property 'Fleets' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active
Warning CS8618 Non-nullable property 'Tariffs' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active
Warning CS8618 Non-nullable property 'BookingAgreements' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code\temp\Release1\temp_1\CarRental\DB\CarRentalContext.cs 14 Active

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
779 questions
{count} votes

Accepted answer
  1. AgaveJoe 29,871 Reputation points
    Apr 28, 2022, 10:27 AM

    How to turn off the warning ?

    Please learn how to use the Visual Studio tooling. Click the lightbulb and select "Declare as nullable" or "Suppress or Configuration issues" to configure the warning.

    But why this is mandatory ? It is giving this warning on every class. why ?

    This is a documented feature in .NET 6 and Visual Studio 2022

    Tutorial: Express your design intent more clearly with nullable and non-nullable reference types

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.