如何调试EF 5 null引用异常?

时间:2013-08-16 15:01:09

标签: entity-framework

当我过滤EF时,我得到一个空引用异常,但我绝对无能为力。

enter image description here

public IEnumerable<TonalityBatchModel> GetTonalityBatch(int briefID)
{
    try
    {
        var brief = NeptuneUnitOfWork.Briefs.FindWhere(b => b.ID == briefID).FirstOrDefault();

        if (brief != null && brief.TonalityCriteria != null)
                    {
                        return brief.TonalityCriteria.TonalityBatches
                                    .Select(b => new TonalityBatchModel()
                                        {
                                            BriefID = b.BriefID,
                                            Status = b.TonalityCriteria.IsActive == true ?"Active":"Ended",
                                            BatchID = b.ID,
                                            CompetitorID = b.BriefCompetitorID,
                                            Competitor = brief.BriefCompetitors.Where(i=>i.ID == b.BriefCompetitorID).Select(c=>c.Organisation.Name).First(),
                                            Size = b.BatchSize,
                                            StartDate = b.StartDate,
                                            EndDate = b.EndDate,
                                            IsPublished = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Published?"Yes":"No",
                                            IsCompleted = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Completed ? "Yes" : "No",
                                            IsAssigned = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Allocated ? "Yes" : "No",
                                            ImportantCount = b.TonalityItems.Count(i=> i.IsImportant),
                                            ArticlesCount = b.TonalityItems.Count,
                                            FavourableCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Favourable),
                                            UnfavourableCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Unfavourable),
                                            NeutralCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Neutral)

                                        }).ToList();
                    }

                    return new List<TonalityBatchModel>();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }

2 个答案:

答案 0 :(得分:1)

您需要将查询缩减为更简单的查询,然后重新开始重新构建,直到NullReferenceException发生。看看你的代码,这里有一些可能的地方(我做了一些假设,因为我不知道你的模型的所有内容):

Competitor = brief.BriefCompetitors.Where(i=>i.ID == b.BriefCompetitorID).Select(c=>c.Organisation.Name).First()

BriefCompetitors可能为null。 c.Organisation可能为空。

IsPublished = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Published?"Yes":"No",

(和其他类似的行)b.Lookup_TonalityBatchStatus可能为空。

ImportantCount = b.TonalityItems.Count(i=> i.IsImportant),

(和其他类似的行)b.TonalityItems可能为空。

答案 1 :(得分:0)

我相信这是因为你的计数正在返回空记录。我可能错了,但是这里生成的SQL就像是:

INNER JOIN TonalityItems i on i.Lookup_TonalityScoreTypes == x

其中x(int)TonalitySourceType.Favourable的值。由于此联接没有匹配的结果,因此无法执行count。您可以尝试将?? 0添加到查询的末尾:

FavourableCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Favourable) ?? 0,