Sep 14, 2017 03:22 PM|gib9898_00|LINK
I have an MVC application using Entity Framework. Part of the application involves creating reports that can be saved to the database. It also involves loading those reports from the database, modifying them, and saving the changes back to the database.
One thing I noticed is that when I save a report, the changes are persisted to the database, but if I re-load that report, the changes aren't there. Obviously, Entity Framework is not updating the reports from the database. I'm wondering how to force Entity
Framework to recognize when a report is old or when it needs to get the most up-to-date version of a report from the database.
Here's how the process flow works:
1) Changes are made to the report on the report configuration page, and the "save" button is clicked.
2) The report data is sent via an AJAX call to the ReportConfigController. The changes are written to the report Entity.
3) A call to _projectService.UpdateProjectForReport(project) is made passing in the project to which the report belongs (i.e. project.Reports is an ICollection<Report> which includes the updated report).
4) UpdateProjectForReport() looks like this:
public void UpdateProjectForReport(Project project)
5) Looking at the database, I verify that the changes have been persisted.
6) I navigate away from the report configuration page and I come back.
7) I select the report I just updated from a drop down menu. This triggers an AJAX call to load the report.
8) The ReportConfigController makes an API call to get the report, passing in the project ID and the report ID.
9) The API gets the report as follows:
_project = _projectService.GetProject(projectId);
Report report = _project.Report.Where(r => r.ReportId == reportId).FirstOrDefault();
...where _project.Report is an ICollection<Report>.
10) _projectService.GetProject() looks like this:
public Project GetProject(int projectId)
return _context.Projects.Include("Report").FirstOrDefault(p => p.ProjectId == projectId);
11) The report is converted to json and returned.
It's at steps 9) and 10) where I need Entity Framework to recognize that it needs to get the most up-to-date reports for the project. It isn't doing so.
Can anybody help me with this?