C# – Entity Framework – Write DbContext Changes To Console

Issue

Debug DbContext issues easier with this extension which writes the changes to the console window.

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, a city renamed and a country is 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. You can download the entire example by clicking the icon in the top right corner.