Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:41:56+00:00 2026-06-05T21:41:56+00:00

I need to query one object with its related objects, I am using the

  • 0

I need to query one object with its related objects, I am using the unit of work pattern and repository pattern.

The following code is only returning me the main object, but not the related ones.

public List<EcoBonusRequest> GetAllRequestsWaitForPayment()
            {
                return _context.EcoBonusRequests.Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment).Include("Dealer").Include("Vehicle").ToList();
            }

So I am getting a null reference exception when I try use the Dealer and the Vehicle of that object on my page

public void Databind(EcoBonusRequest ecoBonusRequest)
        {
            if (ecoBonusRequest != null)
            {
                LblRegistrationNumberValue.Text = ecoBonusRequest.Dealer.Nuteres; ---> Exception here

Repository

public class UnitOfWork : IDisposable
    {
        #region Variables
            private readonly AskAndTrackContext _context = new AskAndTrackContext();
            private RequestBaseRepository _requestBaseRepository;
            private EcoBonusRequestRepository _ecobonusworkflowRepository;

 public RequestBaseRepository RequestBaseRepository
            {
                get
                {
                    return _requestBaseRepository ??
                            (_requestBaseRepository = new RequestBaseRepository(_context));
                }
            }

            public EcoBonusRequestRepository EcoBonusRequestRepository
            {
                get
                {
                    return _ecobonusworkflowRepository ??
                            (_ecobonusworkflowRepository = new EcoBonusRequestRepository(_context));
                }
            }

Update1: Entities

public class RequestBase
    {
        public int RequestBaseId { get; set; }
        public string CurrentStatus { get; set; }
        public string RequestNumber { get; set; }

        [Column(TypeName = "Date")]
        public DateTime RequestDate { get; set; }
        public bool IsOnHold { get; set; }

        public virtual Dealer Dealer { get; set; }
        public virtual Requester Requester { get; set; }
        public virtual Vehicle Vehicle { get; set; }

        public virtual ICollection<Attachment> Attachments { get; set; }
        public virtual ICollection<WorkflowHistory> WorkflowHistories { get; set; }


 public class EcoBonusRequest : RequestBase
    {
        public string BrandReturnedVehicle { get; set; }

        public string TypeReturnedVehicle { get; set; }

        public string ChassisReturnedVehicle { get; set; }

DbContext

public class AskAndTrackContext : DbContext
    {
        public AskAndTrackContext() : base("AskAndTrack")
        {
            //It doesnt create the database.
            //Database.SetInitializer<AskAndTrackContext>(null);
            //Database.SetInitializer<AskAndTrackContext>(new AskAndTrackDevInitializer());
        }

        public DbSet<RequestBase> RequestBases { get; set; }
        public DbSet<EcoBonusRequest> EcoBonusRequests { get; set; }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:41:58+00:00Added an answer on June 5, 2026 at 9:41 pm

    Well, Include means that the referenced entity will be eager loaded.

    But if the referenced entity is nullable (and null in your query), query won’t be able to retrieve a non existing referenced entity.

    So if ecoBonusRequest.Dealer == null… You will get a NRE.

    EDIT : could you try to test this way ?

    public List<EcoBonusRequest> GetAllRequestsWaitForPayment()
                {
                    var query = _context.EcoBonusRequests.Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment).Include("Dealer").Include("Vehicle");
                    var dealerExists = query.All(m => m.Dealer != null);
                    return query.ToList();
                }
    

    EDIT 2:

    Maybe a problem of inheritance and Include
    Can you try

    return _context.RequestBase
                   .Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment)
                   .Include("Dealer")
                   .Include("Vehicle")
                   .OfType<EcoBonusRequests>()
                   .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following linq query which returns a Report Object; yet I need
I need to make a query to one of Google's services. I read this
I need this query - update last but one record. UPDATE changes SET checked=''
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I need help extending a functional update query that performs calculations on one record
Need to query my SQL server from Access using an ADO connection (for example),
I need query to get data between two days.I tried with this.But it gives
I need to query statistics about duplicate values in my database table. For example,
I need to query large amount of data from a database via C# and
I need a query to search phone number. Simple searching is fine, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.