question

mehmoodtekfirst-9443 avatar image
0 Votes"
mehmoodtekfirst-9443 asked AgaveJoe answered

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

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://docs.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



dotnet-entity-framework-core
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Declare a nullable types or turn off the warning.

 public DbSet<FranchiseWebInquiry>? FranchiseWebInquiries { get; set; }



0 Votes 0 ·

How to turn off the warning ? But why is it coming in default case ?

It is giving this warning on every class. why ?

197323-image.png


Now I need to handle the things by checking null each time

197351-image.png


0 Votes 0 ·
image.png (187.3 KiB)
image.png (338.3 KiB)

1 Answer

AgaveJoe avatar image
1 Vote"
AgaveJoe answered

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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.