C# – Entity Framework – Write DbContext Changes To Console

Issue

There are times when knowing what changes have occurred within a DbContext is useful for debugging data or event issues. Each time I needed to see what the DbContext changes were, I’d write the same few lines of code. After doing this enough times, I created a method, which grew into the one in the zip file you can download by clicking the button in the upper right corner of this page.

Solution

When debugging I typically place WriteDbContextChangesToConsole() just before SaveChanges() to see and understand what is about the be committed to the database. Other times, I place WriteDbContextChangesToConsole() in the code after an event to see if it did what was expected. Since it is an extension of DbContext it is easy to place anywhere.

In this example, a country is added, renamed and removed.



Console.WriteLine("Add a new country Milo with capital Sammi City.");
_dbContext.CountryCapitals.Add(new CountryCapital { CountryName = "Milo", CapitalName = "Sammi City" });

Console.WriteLine("Rename a capital to Codyville.");
_dbContext.CountryCapitals.Single(x => x.Id == 66).CapitalName = "Codyville";

Console.WriteLine("Remove a country.");
_dbContext.CountryCapitals.Remove(_dbContext.CountryCapitals.Single(x => x.Id == 100));

// Write changes to DbContext to console window.
_dbContext.WriteDbContextChangesToConsole();

_dbContext.SaveChanges();

Calling _dbContext.WriteDbContextChangesToConsole() produces

Output
_______________________________________________________________________________________________________________________
CountryCapital:
_______________________________________________________________________________________________________________________
                 Property Name |               Action |                 Original Value |                  Current Value
_______________________________________________________________________________________________________________________
                            Id |                Added |                              - |                    -2147482451
                   CapitalName |                Added |                              - |                     Sammi City
                   CountryName |                Added |                              - |                           Milo
_______________________________________________________________________________________________________________________
CountryCapital:
_______________________________________________________________________________________________________________________
                 Property Name |               Action |                 Original Value |                  Current Value
_______________________________________________________________________________________________________________________
                   CapitalName |             Modified |                          Accra |                      Codyville
_______________________________________________________________________________________________________________________
CountryCapital:
_______________________________________________________________________________________________________________________
                 Property Name |               Action |                 Original Value |                  Current Value
_______________________________________________________________________________________________________________________
                            Id |              Deleted |                            100 |                              -
                   CapitalName |              Deleted |                        Tripoli |                              -
                   CountryName |              Deleted |                          Libya |                              -

Conclusion

WriteDbContextChangesToConsole() is a useful tool to easily see and understand what DbContext changes are occurring in your application.