Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DbContext.Set<T>().FromSql() not updated #9331

Closed
JeanCollas opened this issue Aug 3, 2017 · 3 comments
Closed

DbContext.Set<T>().FromSql() not updated #9331

JeanCollas opened this issue Aug 3, 2017 · 3 comments
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@JeanCollas
Copy link

JeanCollas commented Aug 3, 2017

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();
@anpete
Copy link
Contributor

anpete commented Aug 4, 2017

@JeanCollas Which version of EF Core?

@JeanCollas
Copy link
Author

EF Core 1.1.2 and ASP.NET Core 1.1.1

@divega divega assigned smitpatel and unassigned smitpatel Aug 4, 2017
@divega
Copy link
Contributor

divega commented Aug 11, 2017

@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:

  1. Bypass the identity map by adding AsNoTracking() to the queries, as you already found out.
  2. Make sure the stored procedures return distinct or unique key values.
  3. Use separate DbContext instances to execute the two queries.
  4. When we finish implementing the feature, map Stats_ViewsPerHourLine as a view type.

Feel free to reactivate if this doesn't answer your question.

@divega divega closed this as completed Aug 11, 2017
@divega divega added the closed-no-further-action The issue is closed and no further action is planned. label Aug 11, 2017
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

5 participants