question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

EventEntry.ToJson() tracks all properties at changes when Entity.State sets to Modified

See original GitHub issue

Hey Federico šŸ‘‹

I realize one thing. It may work as expected fine but I donā€™t think so.

Before we are updating an entity. If we set our entity objectā€™s EntityState as Modified. Audit writes as all the properties into Jsonā€™s changes array. Actually, I only changed 1 or 2 property.

Letā€™s see with an example;

public class Category : AuditableEntity<int>
{
    public string Title { get; set; }

    public string Description { get; set; }

    // .. more prop goes ..
}
// Let's say I retrieved category object from the data source in the category variable
category.Title = "new updated title"; 
// We don't updated any other props.
_context.Entry(category).State = EntityState.Modified;
await _context.SaveChangesAsync()

My expectation to see only category title in the Jsonā€™s changes array but all of the category property will be there. šŸ˜”

Thanks for the great projects and helps! šŸš€šŸš€šŸš€

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
arslanaybarscommented, Aug 4, 2019

Yes, you are right. But if I donā€™t set Entity State as modified (I think even if we set it should not break changes), somehow changeTracker.Entries() elements are not set to modified. I also override SaveChanges. If my entity updated I need to know it to update run some internal logic.

But to anyone usage, I solve it by using the following lines before called base.SaveChangesAsync(cancellationToken);

foreach (var e in changedEntity.Properties.Where(x => x.IsModified))
{
    e.CurrentValue = e.CurrentValue ?? "null";
    e.OriginalValue = e.OriginalValue ?? "null";
    e.IsModified = !e.CurrentValue.Equals(e.OriginalValue);
}

I also donā€™t get it why it is happening. I can close the issue, probably it is a specific case donā€™t know why?

Thanks for your time.

0reactions
thepirat000commented, Aug 4, 2019

I donā€™t get itā€¦ If you only change the Title property, why would you want to track the unmodified properties?

This should be enough:

var category = context.Categories.FirstOrDefault();
category.Title = "new title";
context.SaveChanges();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework - Why explicitly set entity state to modified?
It is purpose of change tracking to find that you have changed a value on attached entity and put it to modified state....
Read more >
Working with entity states - EF6
Modified : the entity is being tracked by the context and exists in the database, and some or all of its property values...
Read more >
JS | Change Tracking
Change tracking. Breeze entities are ā€œself-trackingā€ which means that each entity instance keeps track of its own changed state ā€¦ and much more....
Read more >
Configuration Management Platform Wireless User's Guide
Viewing Subscriber Entity States Associated with a Subscriber ... The audit log records all notification server actions (create, modify, and delete), policy.
Read more >
Monitoring and Diagnostics
The Time Line allows you to view status and health information for a specific point in time or across a range of time....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found