In a table I have implemented a database trigger to populate a column with a calculated value (column is named ProjectCode). I use Web API controllers to create and retrieve entries from the database. An instance of my DbContext is injected into the controller.
When DbContexts adds a new instance of the entity and then saves, the returned data does not include the automatically created value for the column. The database does show a correct value in the column for the newly inserted row. Even on retrieving the data within the same method the property shows a value of null.
Project project = new Project();
dbContext.Projects.Add(project);
await dbContext.SaveChangesAsync();
// When placing a debugger stop here, and checking the database, I can see that the value for ProjectCode is populated correctly by the database trigger
// project.Id has valid value, however project.ProjectCode is null
Project createdProject = await dbContext.SingleOrDefaultAsync(p => p.Id == project.Id);
// createdProject is not null, however createdProject.ProjectCode is null
After the web call is completed, and the controller and DbContext are dispose, a new web API all is made to retrieve the data. DbContext is used in identical fashion by calling await dbContext.SingleOrDefaultAsync(p => p.Id == project.Id)
, the ProjectCode the shows correctly populated.
Why would it not show correctly populated after adding or immediately retrieving even though the row in teh database shows it’s populated?