。包括不在Entity Framework上工作

时间:2012-06-19 13:17:29

标签: c# linq entity-framework entity-framework-4.1

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

以下代码仅返回主要对象,但不返回相关的代码。

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

所以当我尝试在我的页面上使用Dealer和该对象的Vehicle时,我得到一个空引用异常

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

Update1:​​实体

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

2 个答案:

答案 0 :(得分:2)

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

return _context.EcoBonusRequests.Include("Dealer").Include("Vehicle").Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment).ToList();

答案 1 :(得分: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

可能是继承和包含的问题 你能试试吗

return _context.RequestBase
               .Where(p => p.CurrentStatus == RequestStatus.WaitingForPayment)
               .Include("Dealer")
               .Include("Vehicle")
               .OfType<EcoBonusRequests>()
               .ToList();