Ormlite - get the insertable and updatable columns and values

I’m trying to figure out the best way to get the insertable and updateable columns and values on a class inside an orm lite filter. I basically need to loop over each column to insert its value into a table one row per column in both cases of insert and update. For an update I need to get the original record and then insert the original and new values into the same table.

Something like this:

public class AuditLogDetails
    {
        [PrimaryKey]
        [AutoIncrement]
        public int Id { get; set; }
        public string ColumnName { get; set; }
        public string OriginalValue { get; set; }
        public string NewValue { get; set; }
        public int AuditLogId { get; set; }
    }

Filter code. How can loop over each row’s insertable/updatable properties to build the old and new values?

                var queryType = row.GetType();
                var modelDef = queryType.
                var dialectProvider = dbCmd.GetDialectProvider();
                var tableName = dialectProvider.NamingStrategy.GetTableName(modelDef);

                var colNames = modelDef.GetColumnNames(dialectProvider);
                var key = modelDef.GetPrimaryKey(row);

                var existing = dbCmd.Connection.UnTypedSingleById(queryType, key);

I think I figured this out. Code posted for anyone else that might be looking to show an audit log of original and new values:

OrmLiteConfig.UpdateFilter = (dbCmd, row) => {
            if (row.GetType().GetCustomAttribute<NoTrackAttribute>() != null) return;  // simple attribute to denote not to track the class
            try
            {
                var queryType = row.GetType();
                var modelDef = queryType.GetModelMetadata();
               
                //var name = modelDef.ModelName;
                var dialectProvider = dbCmd.GetDialectProvider();
                var tableName = dialectProvider.NamingStrategy.GetTableName(modelDef);

                var key = modelDef.GetPrimaryKey(row);
                var existing = dbCmd.Connection.UnTypedSingleById(queryType, key);
                var auditLog = new AuditLog();
                auditLog.EventDateUTC = DateTime.UtcNow;
                auditLog.TableName = tableName;
                auditLog.EventType = 2;  // This is hard coded 
                auditLog.RecordId = key.ToString();
                var identity = dbCmd.Connection.Insert<AuditLog>(auditLog, selectIdentity: true);
                List<AuditLogDetails> details = new List<AuditLogDetails>();
                foreach (var def in modelDef.FieldDefinitions)
                {
                 
                    if (def == null || def.IsComputed || def.IgnoreOnUpdate) continue;
                    
                    
                    var record = new AuditLogDetails()
                    {
                        AuditLogId = (int)identity,
                        ColumnName = !String.IsNullOrEmpty(def.Alias) ? def.Alias : def.Name,
                        NewValue =  def.GetValue(row)?.ToString() ?? "",
                        OriginalValue = def.GetValue(existing)?.ToString() ?? "",
                    };

                        dbCmd.Connection.Insert<AuditLogDetails>(
                        record);
                    

                }
            }
            catch (Exception ex)
            {

            }

        };
1 Like