Rob None
Rob None

Reputation: 581

Compare complex object and get difference C# .NET Core 3.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);
    }
}

Upvotes: 1

Views: 3797

Answers (4)

TeaBaerd
TeaBaerd

Reputation: 1179

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.

Upvotes: 2

riezebosch
riezebosch

Reputation: 2026

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.

Upvotes: 1

Givko
Givko

Reputation: 380

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 .

Upvotes: 2

Anoop Narayan
Anoop Narayan

Reputation: 121

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);

Upvotes: 2

Related Questions