Search code examples
sql-serverentity-framework-core

EF Core receives result from database without values from database trigger


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 DbContext adds a new instance of the entity and then saves it, 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 disposed, a new Web API call is made to retrieve the data. DbContext is used in identical fashion by calling

await dbContext.SingleOrDefaultAsync(p => p.Id == project.Id)

the ProjectCode then shows it is correctly populated.

Why would it not show correctly populated after adding or immediately retrieving even though the row in the database shows it's populated?


Solution

  • If you have a calculated column, you can configure EF Core to always retrieve this column while preventing it from being used in INSERT and UPDATE operations:

    modelBuilder.Entity<Project>()
        .Property(e => e.ProjectCode)
        .ValueGeneratedOnAddOrUpdate();