Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/424529411086770177
added 1 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I have the following code to log changes using Entity Framework 6 At.

At the moment it only logs changes to a particular table - but I intend to expand it to cope with any table. I aren't I'm not sure of the best way to extract the table name.

I call this code before DbDb.SaveChanges.SaveChanges Db Db is the DBContext.

private void LogHistory()
    {
        var entries = this.Db.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
            .ToList();
        foreach (var entry in entries)
        {
            foreach (string o in entry.CurrentValues.PropertyNames)
            {
                var property = entry.Property(o);
                var currentVal = property.CurrentValue == null ? "" : property.CurrentValue.ToString();
                var originalVal = property.OriginalValue == null ? "" : property.OriginalValue.ToString();

                if (currentVal != originalVal)
                {
                    if (entry.Entity.GetType().Name.Contains("PropetyPair"))
                    {
                        //  make and add log record
                    }
                }
            }
        }
    }

I have the following code to log changes using Entity Framework 6 At the moment it only logs changes to a particular table - but I intend to expand it to cope with any table. I aren't sure of the best way to extract the table name.

I call this code before Db.SaveChanges Db is the DBContext.

private void LogHistory()
    {
        var entries = this.Db.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
            .ToList();
        foreach (var entry in entries)
        {
            foreach (string o in entry.CurrentValues.PropertyNames)
            {
                var property = entry.Property(o);
                var currentVal = property.CurrentValue == null ? "" : property.CurrentValue.ToString();
                var originalVal = property.OriginalValue == null ? "" : property.OriginalValue.ToString();

                if (currentVal != originalVal)
                {
                    if (entry.Entity.GetType().Name.Contains("PropetyPair"))
                    {
                        //  make and add log record
                    }
                }
            }
        }
    }

I have the following code to log changes using Entity Framework 6.

At the moment it only logs changes to a particular table - but I intend to expand it to cope with any table. I'm not sure of the best way to extract the table name.

I call this code before Db.SaveChanges. Db is the DBContext.

private void LogHistory()
    {
        var entries = this.Db.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
            .ToList();
        foreach (var entry in entries)
        {
            foreach (string o in entry.CurrentValues.PropertyNames)
            {
                var property = entry.Property(o);
                var currentVal = property.CurrentValue == null ? "" : property.CurrentValue.ToString();
                var originalVal = property.OriginalValue == null ? "" : property.OriginalValue.ToString();

                if (currentVal != originalVal)
                {
                    if (entry.Entity.GetType().Name.Contains("PropetyPair"))
                    {
                        //  make and add log record
                    }
                }
            }
        }
    }
edited tags
Link
James Khoury
  • 3.2k
  • 1
  • 25
  • 52
deleted 61 characters in body
Source Link
Kirsten
  • 443
  • 1
  • 5
  • 18

I have the following code to log changes using Entity Framework 6 At the moment it only logs changes to a particular table - but I intend to expand it to cope with any table. I aren't sure of the best way to extract the table name.

A strategy for capturing the windows user would be handy too.

I call this code before Db.SaveChanges Db is the DBContext.

private void LogHistory()
    {
        var entries = this.Db.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
            .ToList();
        foreach (var entry in entries)
        {
            foreach (string o in entry.CurrentValues.PropertyNames)
            {
                var property = entry.Property(o);
                var currentVal = property.CurrentValue == null ? "" : property.CurrentValue.ToString();
                var originalVal = property.OriginalValue == null ? "" : property.OriginalValue.ToString();

                if (currentVal != originalVal)
                {
                    if (entry.Entity.GetType().Name.Contains("PropetyPair"))
                    {
                        //  make and add log record
                    }
                }
            }
        }
    }

I have the following code to log changes using Entity Framework 6 At the moment it only logs changes to a particular table - but I intend to expand it to cope with any table. I aren't sure of the best way to extract the table name.

A strategy for capturing the windows user would be handy too.

I call this code before Db.SaveChanges Db is the DBContext.

private void LogHistory()
    {
        var entries = this.Db.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
            .ToList();
        foreach (var entry in entries)
        {
            foreach (string o in entry.CurrentValues.PropertyNames)
            {
                var property = entry.Property(o);
                var currentVal = property.CurrentValue == null ? "" : property.CurrentValue.ToString();
                var originalVal = property.OriginalValue == null ? "" : property.OriginalValue.ToString();

                if (currentVal != originalVal)
                {
                    if (entry.Entity.GetType().Name.Contains("PropetyPair"))
                    {
                        //  make and add log record
                    }
                }
            }
        }
    }

I have the following code to log changes using Entity Framework 6 At the moment it only logs changes to a particular table - but I intend to expand it to cope with any table. I aren't sure of the best way to extract the table name.

I call this code before Db.SaveChanges Db is the DBContext.

private void LogHistory()
    {
        var entries = this.Db.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
            .ToList();
        foreach (var entry in entries)
        {
            foreach (string o in entry.CurrentValues.PropertyNames)
            {
                var property = entry.Property(o);
                var currentVal = property.CurrentValue == null ? "" : property.CurrentValue.ToString();
                var originalVal = property.OriginalValue == null ? "" : property.OriginalValue.ToString();

                if (currentVal != originalVal)
                {
                    if (entry.Entity.GetType().Name.Contains("PropetyPair"))
                    {
                        //  make and add log record
                    }
                }
            }
        }
    }
Source Link
Kirsten
  • 443
  • 1
  • 5
  • 18
Loading