FirstOrDefault throws a NullReferenceException

Given the code:

using (MyContext ctx = new MyContext())
{
    // Returns true:
    var any = ctx.AggregateListAnswers.Any(); 
    // Throws NullReferenceException:
    var fod = ctx.AggregateListAnswers.FirstOrDefault();
}

      

The caller line .FirstOrDefault()

issues NullReferenceException

, and the caller line .Any()

returns true

.

The mapping for AggregateListAnswer

is defined as follows:

modelBuilder.Entity<AggregateListAnswer>().ToTable("UccAggregate_ListAnswers");

      

After initial creation, the table is dropped and an indexed view with the same name is created . There is a lot of data in the view.

What could be causing this? How do I fix the problem NullReferenceException

?

UPDATE

I can work around the problem by using object projection with the same properties, but no attributes [KeyAttribute]

:

var materialized = ctx.Set<AggregateListAnswer>
                      .Select(a => new AggregateListAnswerNoKey()
                      {
                          PropA = a.PropA,
                          PropB = a.PropB
                      }).ToList()

      

Here is a class, with method names abbreviated to obfuscate some of the domain information:

public class AggregateListAnswer
{
    [Key, Column(Order = 0)]public virtual int? PY { get; set; }
    [Key, Column(Order = 1)]public virtual short? CC { get; set; }
    [Key, Column(Order = 2)]public virtual short? BC { get; set; }
    [Key, Column(Order = 3)]public virtual short? MC { get; set; }
    [Key, Column(Order = 4)]public virtual short? SC { get; set; }
    [Key, Column(Order = 5)]public virtual short? BSC { get; set; }
    [Key, Column(Order = 6)]public virtual short? FTC { get; set; }
    [Key, Column(Order = 7)]public virtual short? MTC { get; set; }
    [Key, Column(Order = 8)]public virtual short? DTC { get; set; }
    [Key, Column(Order = 9)]public virtual int RQId { get; set; }
    [Key, Column(Order = 9)]public virtual Question RQ { get; set; }
    [Key, Column(Order = 10)]public virtual int NumericValue { get; set; }
    [Key, Column(Order = 11)]public virtual short? SeC { get; set; }

    public long Cnt { get; set; }

    public double Wgt { get; set; }
}

      

Please note that Cnt

and are Wgt

not intended to be saved. They are temporary properties. The key is so large that it matches the columns of the existing indexed view. The two properties are annotated with the same key order of 9 because they are the same thing (identifier and the object to which the identifier belongs).

+3


source to share


1 answer


The difference between Any

and projection, on the one hand, and FirstOrDefault

, on the other hand, is that in the latter case the object is realized AggregateListAnswer

. Any

only returns a boolean and projects some other object. Thus, NRE must be called by object creation. The first step in troubleshooting might be to check whether the request is complete at all (I think it does). This will mean that the model itself is correct and not associated with display problems.

The presence KeyAttribute

in the referenced property is not general (and optional), but it shouldn't cause any problems either. And I'm assuming you didn't display both transient properties.

How do I resolve the NullReferenceException?



The first thing to look at is the stack trace. I had limited success checking the EF source code where the exceptions were thrown. Sometimes this gives a clue.

Your best bet is to compile the EF source code in debug mode and replace the current reference in the project with a compiled one. I did it once and it put me on the right track. (I usually do it wrong).

+1


source







All Articles