0

I'm trying to add auditing to my application. The problem that I encountered is that I can not get some old and new values from non primitive types. For example, if I have an entity Person, I can easily get name, age, but I can not get any property of the Country:

public class Person
{
    public string Name { get; set;}
    public int Age { get; set; }
    public Country { get;set;}
}

and country might look like:

public class Country
{
    public int Id { get;set;}
    public string Name { get;set;}
}

Currently the code that returns new/old value looks like (dbEntry is DbEntityEntry):

foreach (var prop in dbEntry.CurrentValues.PropertyNames)
{
    string oldValue = dbEntry.OriginalValues.GetValue<object>(prop) == null ? null : dbEntry.OriginalValues.GetValue<object>(prop).ToString();
    string newValue = dbEntry.CurrentValues.GetValue<object>(prop) == null ? null : dbEntry.CurrentValues.GetValue<object>(prop).ToString();
}

How can I get information related to the country (I want to have country name in this case)?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Daemon025
  • 87
  • 1
  • 11

1 Answers1

0

Have you tried existing solutions ?

https://www.nuget.org/packages/TrackerEnabledDbContext

Bilal Fazlani
  • 6,727
  • 9
  • 44
  • 90
  • Nope, I implemented my own solution. Does your suggested library track any changes in collections? – Daemon025 Nov 02 '14 at 12:55
  • If you mean does it track entities that are in collections, then yes. I wrote it my self btw. Although i am not sure if i understand your question correctly.. – Bilal Fazlani Nov 08 '14 at 08:45
  • Here is another one: [Audit.EntityFramework](https://www.nuget.org/packages/Audit.entityframework/) – thepirat000 Sep 09 '16 at 04:37