Reputation: 34038
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; }
Upvotes: 1
Views: 9547
Reputation: 60503
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();
Upvotes: 2
Reputation: 1746
Try moving the includes to before the where, like this
return _context.EcoBonusRequests.Include("Dealer").Include("Vehicle").Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment).ToList();
Upvotes: 1