mishrsud / mvc-mini-profiler

Automatically exported from code.google.com/p/mvc-mini-profiler
0 stars 0 forks source link

NullReferenceException in EF project #57

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
A:  I uploaded a project, when you run it, at first time show page correctly, 
then you refresh the page in browser, it will show the Exception.

What version of the product are you using? On what operating system?
A:  I use 1.5 version now, but in 1.4 the problem was also there.

Please provide any additional information below.
A:  the NullReferenceException throw at the line of Head.AddSqlTiming(stats); 
the Head is null.

        internal void AddSqlTiming(SqlTiming stats)
        {
            int count;

            stats.IsDuplicate = _sqlCounts.TryGetValue(stats.RawCommandString, out count);
            _sqlCounts[stats.RawCommandString] = count + 1;

            HasSqlTimings = true;
            if (stats.IsDuplicate)
            {
                HasDuplicateSqlTimings = true;
            }

            Head.AddSqlTiming(stats);
        }

Original issue reported on code.google.com by tocha...@gmail.com on 19 Jul 2011 at 7:56

Attachments:

GoogleCodeExporter commented 8 years ago
I find this problem is because EF 4.1 will cache DbSet's DbCommandDefinition, 
so when the 2nd time show the page, it will use the profile of last time. 
CreateDbCommandDefinition is only called at first time.

So, the statement as below will throw the exception:

return View(ctx.Posts);

If change DbSet to IQueryable as below, it will not throw the exception:

return View(ctx.Posts.Select(p=>p));

I wonder if there is some way to fix the problem?

Original comment by tocha...@gmail.com on 19 Jul 2011 at 9:13

GoogleCodeExporter commented 8 years ago
I find another condition that will fire the exception.

that is when EF need iterate virtual (lazy-loading) ICollection. For example

 @foreach (var tag in post.Tags)
 {
    <li>@tag.Name </li>
 }

Original comment by tocha...@gmail.com on 19 Jul 2011 at 9:59

GoogleCodeExporter commented 8 years ago
I bet it calls .Connection= on the command ... we could set 
profiler=MvcProfiler.Current at that point. 

Original comment by sam.saff...@gmail.com on 19 Jul 2011 at 10:55

GoogleCodeExporter commented 8 years ago
on second thought, I am not sure about this ... reusing db commands is really 
dangerous ... 

Original comment by sam.saff...@gmail.com on 19 Jul 2011 at 11:16

GoogleCodeExporter commented 8 years ago
I had a repro ... and I think I sorted this one out now. command reuse is risky 
but I assume they "check it out" internally so you do not have multiple 
consumers setting connection on the same object. 

Original comment by sam.saff...@gmail.com on 20 Jul 2011 at 12:35

GoogleCodeExporter commented 8 years ago
Really perfect!

Original comment by tocha...@gmail.com on 20 Jul 2011 at 2:02