question

mehmoodtekfirst-9443 avatar image
0 Votes"
mehmoodtekfirst-9443 asked mehmoodtekfirst-9443 commented

Eager Loading with Nullable nested dependents

Hi , I am trying to load the dependent objects under franchise object.
Here is my code.



   Franchise? franchiseRp = await _dbContext.Franchise
                     .Include( c=> c.Contact)
                     .ThenInclude(cn => cn.ContactNumbers)
                     .Include(c => c.Contact)
                     .ThenInclude(ca => ca.Contacts_Addresses)
                     .Include(c => c.Contact)
                     .ThenInclude(emailA => emailA.EmailAddresses)
                     .Include(c => c.Contact)
                     .ThenInclude( cad => cad.Contacts_Addresses)
                     .Include(ft => ft.FranchiseThresholds)
                     .ThenInclude(fee => fee.FeeThreshold)
                     .Include(fvcr => fvcr!.Franchise_VCRates)
                      .AsNoTracking()
                     .FirstOrDefaultAsync(f => f.Id == id);



and I am getting this warning .

These dependent entities are nullable entitiy

and the error is as follow

 Severity    Code    Description    Project    File    Line    Suppression State
 Warning    CS8620    Argument of type 'IIncludableQueryable<Franchise, ICollection<FranchiseThreshold>?>' cannot be used for parameter 'source' of type 'IIncludableQueryable<Franchise, IEnumerable<FranchiseThreshold>>' in 'IIncludableQueryable<Franchise, FeeThreshold?> EntityFrameworkQueryableExtensions.ThenInclude<Franchise, FranchiseThreshold, FeeThreshold?>(IIncludableQueryable<Franchise, IEnumerable<FranchiseThreshold>> source, Expression<Func<FranchiseThreshold, FeeThreshold?>> navigationPropertyPath)' due to differences in the nullability of reference types.    CarRentalWidget.BLL    D:\Code\R1\RentalWidget.BLL\DAL\FranchiseRespository.cs    415    Active


and this is the franchise entity.


  public class Franchise
     {
         [Key]
         public int Id { get; set; }
         public string? Name { get; set; }
         public string? TradingName { get; set; }
         public Nullable<int> FranchiseGroup_Id { get; set; }
         public bool Primary { get; set; }
         public string? SystemId { get; set; }
         public string? AccountCode { get; set; }
         public Nullable<bool> WebDomain { get; set; }
         public Nullable<bool> ParticipateOnWeb { get; set; }
         public string? VATNumber { get; set; }
         public Nullable<short> FleetExceptionPeriod { get; set; }
         public Nullable<int> CreatedBy { get; set; }
         public Nullable<System.DateTime> CreatedOn { get; set; }
         public Nullable<int> ModifiedBy { get; set; }
         public Nullable<System.DateTime> ModifiedOn { get; set; }
         public byte Status_Id { get; set; }
         public double Longitude { get; set; }
         public double Latitude { get; set; }
         public bool IsPracticalInsured { get; set; }
         public Nullable<int> VATRate_Id { get; set; }
         public Nullable<int> IPTRate_Id { get; set; }
         public Nullable<bool> IsFeeTypeFixed { get; set; }
         public Nullable<byte> StepCompleted { get; set; }      
         public Nullable<int> Contact_Id { get; set; }
         public string? CompanyRegNumber { get; set; }
         public string? Code { get; set; }
         public string? ServiceKey { get; set; }
         public bool AutoApproveFleet { get; set; }
         public Nullable<short> WebDomainSavingMode { get; set; }
         public Nullable<double> OnlineBookingDiscount { get; set; }
         public Nullable<bool> InsFeeType { get; set; }
         public Nullable<decimal> InsuranceFeePer { get; set; }
         public Nullable<decimal> InsCoiRate { get; set; }
         public Nullable<decimal> InsuranceMonthlyPrem { get; set; }
         public Nullable<int> InsMinimumFleetSize { get; set; }
         public Nullable<decimal> FixedInsuranceFee { get; set; }
         public Nullable<decimal> DailyAmountPVThoseEffect { get; set; }
         public Nullable<System.DateTime> InsuranceHmEndDate { get; set; }
         public string? ClientKey { get; set; }
         public string? FranchiseEmail { get; set; }
         public Nullable<bool> IsFirstMidSubmitted { get; set; }
         public string? CreatedByName { get; set; }
         public string? ModifiedByName { get; set; }
         public Nullable<bool> IsMobileAppEnable { get; set; }
         public string? AccountName { get; set; }
         public string? AccountNo { get; set; }
         public string? SortCode { get; set; }
         public string? GeneralTextField { get; set; }        
         public virtual ICollection<BKPayment>? BKPayments { get; set; }        
         public virtual ICollection<BKRenter>? BKRenters { get; set; }        
         public virtual ICollection<BookingAgreement>? BookingAgreements { get; set; }        
         public virtual ICollection<BreakdownProvider>? BreakdownProviders { get; set; }        
         public virtual ICollection<BufferTime>? BufferTimes { get; set; }
         [ForeignKey("Contact_Id")]
         public virtual Contact? Contact { get; set; }        
         public virtual ICollection<Enquiry>? Enquiries { get; set; }        
         public virtual ICollection<Fleet>? Fleets { get; set; }        
         public virtual ICollection<Franchise_BankHolidays>? Franchise_BankHolidays { get; set; }        
         public virtual ICollection<Franchise_LinkedContacts>? Franchise_LinkedContacts { get; set; }        
         public virtual ICollection<Franchise_DefaultInsurers>? Franchise_DefaultInsurers { get; set; }        
         public virtual ICollection<Franchise_DepositRule>? Franchise_DepositRule { get; set; }
         [ForeignKey("FranchiseGroup_Id")]
         public virtual FranchiseGroup? FranchiseGroup { get; set; }        
         public virtual ICollection<Franchise_OpeningHours>? Franchise_OpeningHours { get; set; }        
         public virtual ICollection<Franchise_RentalRules>? Franchise_RentalRules { get; set; }        
         public virtual ICollection<Franchise_SubOffices>? Franchise_SubOffices { get; set; }        
         public virtual ICollection<Franchise_VCRates>? Franchise_VCRates { get; set; }        
         public virtual ICollection<FranchiseDisclaimatory>? FranchiseDisclaimatories { get; set; }        
         public virtual ICollection<FranchiseThreshold>? FranchiseThresholds { get; set; }        
         public virtual ICollection<GhostVehicle>? GhostVehicles { get; set; }        
         public virtual ICollection<Franchise_InsurerProviders>? Franchise_InsurerProviders { get; set; }        
         public virtual ICollection<Season>? Seasons { get; set; }        
         public virtual ICollection<SpecialRate>? SpecialRates { get; set; }        
         public virtual ICollection<TermsAndCondition>? TermsAndConditions { get; set; }        
         public virtual ICollection<User>? Users { get; set; }        
         public virtual ICollection<StoreAppUser>? StoreAppUsers { get; set; }        
         public virtual ICollection<AgreementRenter>? AgreementRenters { get; set; } 
     }


Should I mention foreign key for each relation ?





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.

@mehmoodtekfirst-9443, Welcome to Microsoft Q&A, Microsoft doc provide two methods to deal with Nullable Reference Types in EF Core.
Please refer to Non-nullable properties and initialization

First, you could use Constructor binding to do it, like the following:

  public virtual ICollection<FranchiseThreshold> FranchiseThresholds { get; set; }  
  public Franchise(ICollection<FranchiseThreshold> sholds)
     {
         FranchiseThresholds = sholds;
     }

Second, you could use null forgiving operator to do it, like the following:

 public virtual ICollection<FranchiseThreshold> FranchiseThresholds  { get; set; } = null!;






1 Vote 1 ·

OK thank you

0 Votes 0 ·

0 Answers