You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a Razor page on ASP.NET Core (internal stats/testing), I had some calls to stored procedures:
@inject AppDbContext DbContext
@{
var stats1 = DbContext.Set<Stats_ViewsPerHourLine>().FromSql("Stats_ViewsLastHours_1");
var stats2 = DbContext.Set<Stats_ViewsPerHourLine>().FromSql("Stats_ViewsLastHours_2");
}
<table>
@foreach(var s in stats1)
{
<tr><td>@s.Time</td><td>@s.Count</td></tr>
}
</table>
<table>
@foreach(var s in stats2)
{
<tr><td>@s.Time</td><td>@s.Count</td></tr>
}
</table>
These two tables were identical when the two outputs should be different. Defining the stats2 variable after the first foreach did not change anything.
I had to do the following to avoid this issue: (probably one of the Tracking/ToList is not necessary)
var stats1 = DbContext.Set<Stats_ViewsPerHourLine>().FromSql("Stats_ViewsLastHours_1").AsNoTracking().ToList();
var stats2 = DbContext.Set<Stats_ViewsPerHourLine>().FromSql("Stats_ViewsLastHours_2").AsNoTracking().ToList();
The text was updated successfully, but these errors were encountered:
@JeanCollas without seeing the contents of the stored procedures out best guess is that they return rows with the same values for the key property of Stats_ViewsPerHourLine.
That would cause the execution of the second query to resolve all the instances from the DbContext's identity map instead of materializing new objects with the new data.
These are possible solutions:
Bypass the identity map by adding AsNoTracking() to the queries, as you already found out.
Make sure the stored procedures return distinct or unique key values.
Use separate DbContext instances to execute the two queries.
When we finish implementing the feature, map Stats_ViewsPerHourLine as a view type.
Feel free to reactivate if this doesn't answer your question.
In a Razor page on ASP.NET Core (internal stats/testing), I had some calls to stored procedures:
These two tables were identical when the two outputs should be different. Defining the stats2 variable after the first
foreach
did not change anything.I had to do the following to avoid this issue: (probably one of the
Tracking
/ToList
is not necessary)The text was updated successfully, but these errors were encountered: