1

我需要查询一个对象及其相关对象,我使用的是工作单元模式和存储库模式。

下面的代码只返回我的主要对象,而不是相关的。

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; }
4

2 回答 2

2

好吧, Include 意味着被引用的实体将被预先加载。

但是,如果引用的实体可以为空(并且在您的查询中为 null),则查询将无法检索不存在的引用实体。

因此,如果 ecoBonusRequest.Dealer == null... 您将获得 NRE。

编辑:你能试着用这种方式测试吗?

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();
            }

编辑 2

可能是继承和Include的问题 可以试试

return _context.RequestBase
               .Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment)
               .Include("Dealer")
               .Include("Vehicle")
               .OfType<EcoBonusRequests>()
               .ToList();
于 2012-06-19T13:22:05.393 回答
1

尝试将包含移动到 where 之前,像这样

return _context.EcoBonusRequests.Include("Dealer").Include("Vehicle").Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment).ToList();
于 2012-06-19T13:21:47.437 回答