1

I have two object of the class Profile and I want to compare it to get the difference. To explain better:

    public partial class Profile
    {
        public long Id { get; set; }
        public long VisitorId { get; set; }
        public string Description { get; set; }
        public long Age { get; set; }
        public DateTime? LastUpdate { get; set;}
    }

I want to know in my method the differences between the object oldProfile and newProfile to make a changelog.

For example, if oldProfile had Age = 10 and Description = "old" and newProfile has Age = 11 and Description = "new", I would now this differences to make two different insert in my database:

public void PostChangelogProfileDetail(Profile oldProfile, Profile newProfile)
{
    ProfileDetailChangeLog profileDetailChangeLog = new ProfileDetailChangeLog();

    //COMPARE oldProfile AND newProfile

    foreach ( //difference resulted in the compare)
    {
        profileDetailChangeLog.VisitorId = newProfile.VisitorId;
        profileDetailChangeLog.ModifiedRecord = //name of the attribute modified (Age, Description, etc...)

        _profileDetailChangeLog.Create(profileDetailChangeLog);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rob None
  • 479
  • 2
  • 9
  • 22
  • There are various ways to compare object 1) write your code to compare property by property 2) use a library; for example you can use Json to find the difference using jsondiffpatch.net. Check this answer - https://stackoverflow.com/questions/24876082/find-and-return-json-differences-using-newtonsoft-in-c – user1672994 Jun 01 '20 at 13:55

4 Answers4

2

You are likely looking to use EF Cores Metadata about an object it is tracking to determine the changes to it. I have done something similar to the stackoverflow solution myself in .net core 3 which makes me believe 3.1 should work as well.

There are articles describing approaches for this in the following articles: Getting all changes made to an object in the Entity Framework & ChangeTracker in Entity Framework Core.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
TeaBaerd
  • 1,104
  • 11
  • 16
2

When you call Add or Update ef core returns EntityEntry. From which EntityEntry you can get the current and old values via the OriginalValues(returns the old values) and the CurrentValues(returns the new values) properties.

From where you can compare and log the differences from the original and the current values via Reflection to see the type/name of the property and the value as their return type is PropertyValues .

Givko
  • 350
  • 2
  • 14
1

Install package from nugget Install-Package ObjectsComparer

And then imply compare : var cmpr = new Comparer(); /* compare isSame is the booleab variable and will return true if both objects are same */ IEnumerable diff;

bool isSame = cmpr.Compare(obj1, obj2, out diff);

1

For disconnected entities, I found this solution.

For finding changes on an existing entity:

var existing = context.Find<Item>(1);
if (existing != null) 
{ 
   context.Entry(existing).CurrentValues.SetValues(changed);
}

Its EntityState will be Modified afterwards but only where there are actual changes.

Had the same answer posted here.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
riezebosch
  • 1,950
  • 16
  • 29