0

I have a question about how EntityFramework works because it's doing something odd in an application I am developing. In a nutshell, I am adding a child entity (a report) to a parent entity (a project), saving it to the database, then when I attempt to retrieve it, the child entity (the report) is not there in the parent entity (the project).

Here are the more detailed steps of what I'm doing:

public JsonResult SubmitData(int projectId, string data = null)

    Project project = _projectService.GetProject(projectId);
    Report report = CreateReport(data);
    project.Report.Add(report);
    _projectService.UpdateProjectForReport(project);

    // Rest of code omitted...
}

public class ProjectService : IProjectService    
{
    public void UpdateProjectForReport(Project project)
    {
        _context.SaveChanges();
    }

    // Rest of code omitted... 
}

^ The above code gets run after the user fills out some information on a form and clicks submit. The information gets passed to SubmitData(...) as a json string called "data". The project to which the report is to be saved is retrieved, the json data is converted to a report, the report is added to the project, and the changes to the project are saved to the database.

I have confirmed that the report is in fact saved to the database and is associated with the project.

Then next step involves retrieving the report:

public async Task<HttpResponseMessage> GetTop3ReportCard([FromUri] int 

projectId, [FromUri] int reportId = 0)
{
    _project = _projectService.GetProject(projectId);
    Report report = _project.Report.Where(r => r.ReportId == reportId).FirstOrDefault();

    // Rest of code omitted...
}

But report is always null.

I've verified that reportId in the call to GetTop3ReportCard(...) is not 0. Neither is projectId.

The only way around this is to restart the application in Visual Studio. Once I do that, and then try to retrieve my report, it comes back not null.

But why would an entity like Project not be loaded by EntityFramework with all the reports belonging to it once they are saved to the database? Is there something about the way EntityFramework works that I don't know about here?

Rufus L
  • 36,127
  • 5
  • 30
  • 43
gib65
  • 141
  • 3
  • 16
  • Are you using an IoC container to hold a copy of your context? If so, check on the scope of your context and ProjectService. You might be holding on to an outdated copy of the context. – allen.mn Sep 05 '17 at 22:05
  • How would I be able to tell? And if it is outdated, how do I update it? – gib65 Sep 06 '17 at 15:47

1 Answers1

1

Entity framework uses SQL for its database backing. SQL does not store records or objects inside parent records, it has defined relations and tables for this. When you store an object inside a parent object, entity framework will just store its ID and the original object property will always remain null. You need to store your child object in another table and have to define a separate class for it.

Now to fetch your object, you have two options, either query that object's table to get the object by using its id from the primary table or use the include method.

var rep = Report.FirstOrDefault(r => r.ReportId == reportId);

Or

var data = _project.FirstOrDefault(x => x.Id == someId).Include("Report");

Read my answer here if you want to learn to define the appropriate relation between the two tables/classes through Entity Framework

Afnan Makhdoom
  • 654
  • 1
  • 8
  • 20
  • Thanks Afnan, this worked: var data = _project.FirstOrDefault(x => x.Id == someId).Include("Report"); Just to add some clarity, a huge part of the problem is that I was saving the report to the project in one application and then retrieving it in a completely different application (a web API). Of course the context didn't know about the new report, but your suggestion about using include("Report") seems to update it. – gib65 Sep 06 '17 at 16:12