【问题标题】:NullReferenceException while getting data from navigation property in web-api 2从 web-api 2 中的导航属性获取数据时出现 NullReferenceException
【发布时间】:2016-09-26 03:33:37
【问题描述】:

我在尝试从导航属性中获取数据时收到NullReferenceException

我正在尝试将来自导航属性“EventImage”的数据显示为 JSon。即使元素在属性内部并且引用了所有内容,我也会收到空引用异常。我已经查询了数据库并且得到了预期的结果,但是当我调用控制器的 [get] 操作方法时,只会出现空引用异常。我猜即使没有合适的元素,并且由于一对零或一对一的关系,我应该实现 'EventImage : null' 作为 JSon 结果。

领域模型:

public class EventDetail
{
    [DatabaseGenerated(DatabaseGeneratedOption.None), Key, ForeignKey("Event")]
    public int EventDetailId { get; set; }

    [DisplayFormat(NullDisplayText = "No information")]
    public string EventLocalization { get; set; }

    [DataType(DataType.Date),
     DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true,
         NullDisplayText = "No information")]
    public DateTime? EventDate { get; set; }

    [DisplayFormat(NullDisplayText = "No information")]
    public string EventDescription { get; set; }

    public virtual EventImage EventImage { get; set; }
    public virtual Event Event { get; set; }

    public virtual ICollection<EventPrice> EventPrices { get; set; }
}

public class EventImage
{
    [DatabaseGenerated(DatabaseGeneratedOption.None), Key, ForeignKey("EventDetail")]
    public int EventImageId { get; set; }

    public string EventImageBase64 { get; set; }

    public virtual EventDetail EventDetail { get; set; }
}

逻辑模型:

public class SingleEventDetail
{
    public int EventDetailId { get; set; }

    public string EventLocalization { get; set; }

    public DateTime? EventDate { get; set; }

    public string EventDescription { get; set; }

    public string EventImage { get; set; }
}

public class EventImage
{
    public int EventImageId { get; set; }

    public string EventImageBase64 { get; set; }
}

我在控制器内部调用的方法

public override SingleEventDetail GetById(int id)
    {
        var eventDetail = _eventDetailRepository.GetAll().Select(e => new SingleEventDetail
        {
            EventDetailId = e.EventDetailId,
            EventDate = e.EventDate,
            EventLocalization = e.EventLocalization,
            EventDescription = e.EventDescription,
            EventPrices =
                _eventDetailRepository.GetById(id)
                    .EventPrices.Select(ep => new EventPriceForSingleEventDetail
                    {
                        Amount = ep.Amount,
                        Currency = ep.Currency,
                        IsFullPrice = ep.IsFullPrice
                    }).ToList(),
            EventImage = e.EventImage.EventImageBase64
        }).SingleOrDefault(e => e.EventDetailId == id);

        return eventDetail;
    }

_eventDetailRepository.GetAll()

public IEnumerable<EventDetail> GetAll()
    {
        return _context.EventDetail.ToList();
    }

_eventDetailRepository.GetById(int id)

public EventDetail GetById(int id)
    {
        return _context.EventDetail.ToList().SingleOrDefault(c => c.EventDetailId == id);
    }

【问题讨论】:

    标签: asp.net asp.net-web-api nullreferenceexception


    【解决方案1】:

    我找到了解决方案。延迟加载不适用于这个特定的“EventImage”导航属性,但我不知道为什么。 我已将方法 _eventDetailRepository.GetById(int id) 更改为带有急切登录的方法,现在一切正常。

    public EventDetail GetById(int? id)
        {
            return _context.EventDetail.Include(e => e.EventPrices).Include(e => e.EventImage).SingleOrDefault(e => e.EventDetailId == id);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 2019-09-09
      相关资源
      最近更新 更多