pattern mvc framework first español code c# logging ef-code-first repository dbcontext

c# - mvc - ¿Cómo puedo registrar todos los cambios de entidades durante.SaveChanges() utilizando primero el código EF?



repository pattern (4)

Estoy usando el código EF primero . También estoy usando un repositorio base para todos mis repositorios y un IUnitofWork que se inyecta en los repositorios:

public interface IUnitOfWork : IDisposable { IDbSet<TEntity> Set<TEntity>() where TEntity : class; int SaveChanges(); } public class BaseRepository<T> where T : class { protected readonly DbContext _dbContext; protected readonly IDbSet<T> _dbSet; public BaseRepository(IUnitOfWork uow) { _dbContext = (DbContext)uow; _dbSet = uow.Set<T>(); } //other methods }

Por ejemplo, mi OrderRepository es así:

class OrderRepository: BaseRepository<Order> { IUnitOfWork _uow; IDbSet<Order> _order; public OrderRepository(IUnitOfWork uow) : base(uow) { _uow = uow; _order = _uow.Set<Order>(); } //other methods }

Y lo uso de esta manera:

public void Save(Order order) { using (IUnitOfWork uow = new MyDBContext()) { OrderRepository repository = new OrderRepository(uow); try { repository.ApplyChanges<Order>(order); uow.SaveChanges(); } } }

¿Hay alguna forma de registrar los historiales de cambios de todas las entidades (incluidas sus propiedades de navegación) durante .SaveChanges() ? Quiero registrar los valores originales (antes de que se produzca el guardado), también los valores modificados (después de que se produzca el guardado).


DbContext tiene propiedad ChangeTracker . Puede anular .SaveChanges () en su contexto y registrar los cambios. No creo que el framework de entidades lo pueda hacer por ti. Probablemente, debes detectar cambios directamente en tus clases modelo.


Has asustado a la gente con el requisito extra

Incluir sus propiedades de navegación.

Esto es simplemente un ejercicio no trivial. Y si esto es importante, debe administrar / hacer un seguimiento de los cambios en las referencias con código.

Este es un ejemplo que cubre este tema Deshacer cambios en entidades de marco de entidad

Hay una muestra haciendo cerca de lo que desea aquí. Deshacer cambios Se puede convertir fácilmente para cargar antes y después de las imágenes en otros lugares.

Dada la entrada ObjectState después de llamar a DetectChanges, puede implementar una opción simple entidad por entidad. y por UOW. Pero la versión de navegación / referencias hace que esto sea muy complejo a medida que redactó el requisito.

EDITAR: Cómo acceder a la lista de cambios

public class Repository<TPoco>{ /.... public DbEntityEntry<T> Entry(T entity) { return Context.Entry(entity); } public virtual IList<ChangePair> GetChanges(object poco) { var changes = new List<ObjectPair>(); var thePoco = (TPoco) poco; foreach (var propName in Entry(thePoco).CurrentValues.PropertyNames) { var curr = Entry(thePoco).CurrentValues[propName]; var orig = Entry(thePoco).OriginalValues[propName]; if (curr != null && orig != null) { if (curr.Equals(orig)) { continue; } } if (curr == null && orig == null) { continue; } var aChangePair = new ChangePair {Key = propName, Current = curr, Original = orig}; changes.Add(aChangePair); } return changes; } ///... partial repository shown } // FYI the simple return structure public class ChangePair { public string Key { get; set; } public object Original { get; set; } public object Current { get; set; } }


He anulado el método predeterminado de SaveChanges para registrar los cambios para agregar / actualizar / eliminar en la entidad. Aunque no cubre los cambios de propiedad de navegación.
Basado en este artículo: Uso del marco de la entidad para la auditoría.

public int SaveChanges(string userId) { int objectsCount; List<DbEntityEntry> newEntities = new List<DbEntityEntry>(); // Get all Added/Deleted/Modified entities (not Unmodified or Detached) foreach (var entry in this.ChangeTracker.Entries().Where (x => (x.State == System.Data.EntityState.Added) || (x.State == System.Data.EntityState.Deleted) || (x.State == System.Data.EntityState.Modified))) { if (entry.State == System.Data.EntityState.Added) { newEntities.Add(entry); } else { // For each changed record, get the audit record entries and add them foreach (AuditLog changeDescription in GetAuditRecordsForEntity(entry, userId)) { this.AuditLogs.Add(changeDescription); } } } // Default save changes call to actually save changes to the database objectsCount = base.SaveChanges(); // We don''t have recordId for insert statements that''s why we need to call this method again. foreach (var entry in newEntities) { // For each changed record, get the audit record entries and add them foreach (AuditLog changeDescription in GetAuditRecordsForEntity(entry, userId, true)) { this.AuditLogs.Add(changeDescription); } // TODO: Think about performance here. We are calling db twice for one insertion. objectsCount += base.SaveChanges(); } return objectsCount; } #endregion #region Helper Methods /// <summary> /// Helper method to create record description for Audit table based on operation done on dbEntity /// - Insert, Delete, Update /// </summary> /// <param name="dbEntity"></param> /// <param name="userId"></param> /// <returns></returns> private List<AuditLog> GetAuditRecordsForEntity(DbEntityEntry dbEntity, string userId, bool insertSpecial = false) { List<AuditLog> changesCollection = new List<AuditLog>(); DateTime changeTime = DateTime.Now; // Get Entity Type Name. string tableName1 = dbEntity.GetTableName(); // http://.com/questions/2281972/how-to-get-a-list-of-properties-with-a-given-attribute // Get primary key value (If we have more than one key column, this will need to be adjusted) string primaryKeyName = dbEntity.GetAuditRecordKeyName(); int primaryKeyId = 0; object primaryKeyValue; if (dbEntity.State == System.Data.EntityState.Added || insertSpecial) { primaryKeyValue = dbEntity.GetPropertyValue(primaryKeyName, true); if(primaryKeyValue != null) { Int32.TryParse(primaryKeyValue.ToString(), out primaryKeyId); } // For Inserts, just add the whole record // If the dbEntity implements IDescribableEntity, // use the description from Describe(), otherwise use ToString() changesCollection.Add(new AuditLog() { UserId = userId, EventDate = changeTime, EventType = ModelConstants.UPDATE_TYPE_ADD, TableName = tableName1, RecordId = primaryKeyId, // Again, adjust this if you have a multi-column key ColumnName = "ALL", // To show all column names have been changed NewValue = (dbEntity.CurrentValues.ToObject() is IAuditableEntity) ? (dbEntity.CurrentValues.ToObject() as IAuditableEntity).Describe() : dbEntity.CurrentValues.ToObject().ToString() } ); } else if (dbEntity.State == System.Data.EntityState.Deleted) { primaryKeyValue = dbEntity.GetPropertyValue(primaryKeyName); if (primaryKeyValue != null) { Int32.TryParse(primaryKeyValue.ToString(), out primaryKeyId); } // With deletes use whole record and get description from Describe() or ToString() changesCollection.Add(new AuditLog() { UserId = userId, EventDate = changeTime, EventType = ModelConstants.UPDATE_TYPE_DELETE, TableName = tableName1, RecordId = primaryKeyId, ColumnName = "ALL", OriginalValue = (dbEntity.OriginalValues.ToObject() is IAuditableEntity) ? (dbEntity.OriginalValues.ToObject() as IAuditableEntity).Describe() : dbEntity.OriginalValues.ToObject().ToString() }); } else if (dbEntity.State == System.Data.EntityState.Modified) { primaryKeyValue = dbEntity.GetPropertyValue(primaryKeyName); if (primaryKeyValue != null) { Int32.TryParse(primaryKeyValue.ToString(), out primaryKeyId); } foreach (string propertyName in dbEntity.OriginalValues.PropertyNames) { // For updates, we only want to capture the columns that actually changed if (!object.Equals(dbEntity.OriginalValues.GetValue<object>(propertyName), dbEntity.CurrentValues.GetValue<object>(propertyName))) { changesCollection.Add(new AuditLog() { UserId = userId, EventDate = changeTime, EventType = ModelConstants.UPDATE_TYPE_MODIFY, TableName = tableName1, RecordId = primaryKeyId, ColumnName = propertyName, OriginalValue = dbEntity.OriginalValues.GetValue<object>(propertyName) == null ? null : dbEntity.OriginalValues.GetValue<object>(propertyName).ToString(), NewValue = dbEntity.CurrentValues.GetValue<object>(propertyName) == null ? null : dbEntity.CurrentValues.GetValue<object>(propertyName).ToString() } ); } } } // Otherwise, don''t do anything, we don''t care about Unchanged or Detached entities return changesCollection; }


Puede obtener los valores de antes y después de todas las entidades modificadas en DbContext.ChangeTracker . Desafortunadamente el API es un poco detallado:

var changeInfo = context.ChangeTracker.Entries() .Where (t => t.State == EntityState.Modified) .Select (t => new { Original = t.OriginalValues.PropertyNames.ToDictionary (pn => pn, pn => t.OriginalValues[pn]), Current = t.CurrentValues.PropertyNames.ToDictionary (pn => pn, pn => t.CurrentValues[pn]), });

Puede modificar eso para incluir cosas como el tipo de entidad si lo necesita para su registro. También hay un método ToObject() en DbPropertyValues (el tipo de OriginalValues ​​y CurrentValues) al que podría llamar si ya tiene una forma de registrar objetos completos, aunque los objetos devueltos de ese método no tendrán sus propiedades de navegación completas.

También puede modificar ese código para obtener todas las entidades en el contexto sacando la cláusula Where , si tiene más sentido teniendo en cuenta sus requisitos.