我需要查询一个对象及其相关对象,我使用的是工作单元模式和存储库模式。
下面的代码只返回我的主要对象,而不是相关的。
public List<EcoBonusRequest> GetAllRequestsWaitForPayment()
{
return _context.EcoBonusRequests.Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment).Include("Dealer").Include("Vehicle").ToList();
}
因此,当我尝试在我的页面上使用该对象的经销商和车辆时,我得到一个空引用异常
public void Databind(EcoBonusRequest ecoBonusRequest)
{
if (ecoBonusRequest != null)
{
LblRegistrationNumberValue.Text = ecoBonusRequest.Dealer.Nuteres; ---> Exception here
存储库
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));
}
}
更新1:实体
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; }
数据库上下文
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; }