Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,
I have a problem in Change Tracking in entity framework in modified sate
I have used such as this code
C#
var modifiedEntities = ChangeTracker.Entries()
    .Where(p => p.State == EntityState.Modified).ToList();

var now = DateTime.UtcNow;

foreach (var change in modifiedEntities)
{
    var entityName = change.Entity.GetType().Name;
    foreach (var prop in change.OriginalValues.PropertyNames)
    {
        var originalValue = change.OriginalValues[prop].ToString();
        var currentValue = change.CurrentValues[prop].ToString();
        if (originalValue != currentValue) //Only create a log if the value changes
        {
            //Create the Change Log
        }
    }

The debugger comes into if condition but my problem is the field has been modified but this code could not find it but it knows it has modified. Because it comes into the inner part. How can I get the previous value..
Thanks

What I have tried:

I implemented its code but it could get the previous value.
Posted
Updated 29-Sep-20 22:12pm
v2

1 solution

Try:
C#
var modifiedEntities = ChangeTracker.Entities().Where(p => p.State == EntityState.Modified).ToList();
foreach (var change in modifiedEntities)
{
    var entityName = change.Entity.GetType().Name;
    foreach (string propertyName in change.OriginalValues.PropertyNames)
    {
        if (change.Property(propertyName) is DbPropertyEntry property && property.IsModified)
        {
            object originalValue = property.OriginalValue;
            object currentValue = proeprty.CurrentValue;
            // Create the change log...
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900