/// <summary> /// Gets the audit property. /// </summary> /// <param name="relation">The relation.</param> /// <returns></returns> private TAuditProperty GetAuditProperty(System.Data.Entity.Core.Objects.DataClasses.IRelatedEnd relation) { Helpers.ThrowIfNull(relation != null, "relation"); string relationName = relation.RelationshipSet.Name; string propertyName = string.Empty; // check our list TAuditProperty auditProperty = this.currentAuditProperties.FirstOrDefault(ap => ap.EntityName == relationName && ap.PropertyName == propertyName && ap.IsRelation == true); if (auditProperty == null) { // check the database auditProperty = this.AuditProperties.FirstOrDefault(ap => ap.EntityName == relationName && ap.PropertyName == propertyName && ap.IsRelation == true); if (auditProperty == null) { // create it auditProperty = new TAuditProperty() { EntityName = relationName, PropertyName = propertyName, PropertyType = string.Empty, IsRelation = true }; } this.currentAuditProperties.Add(auditProperty); } return(auditProperty); }
/// <summary> /// Gets the audit property. /// </summary> /// <param name="relation">The relation.</param> /// <returns></returns> private AuditProperty GetAuditProperty(System.Data.Entity.Core.Objects.DataClasses.IRelatedEnd relation) { Contract.Requires <ArgumentNullException>(relation != null, "relation"); Contract.Ensures(Contract.Result <AuditProperty>() != null); string relationName = relation.RelationshipSet.Name; string propertyName = string.Empty; // check our list AuditProperty auditProperty = this.currentAuditProperties.FirstOrDefault(ap => ap.EntityName == relationName && ap.PropertyName == propertyName && ap.IsRelation == true); if (auditProperty == null) { // check the database auditProperty = this.AuditProperties.FirstOrDefault(ap => ap.EntityName == relationName && ap.PropertyName == propertyName && ap.IsRelation == true); if (auditProperty == null) { // create it auditProperty = new AuditProperty() { EntityName = relationName, PropertyName = propertyName, PropertyType = string.Empty, IsRelation = true }; } this.currentAuditProperties.Add(auditProperty); } return(auditProperty); }
/// <summary> /// Creates the audit delete relation records. /// </summary> private void CreateAuditDeleteRelationRecords() { // audit deleted relationships var deletedRelations = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Deleted).Where(ose => ose.IsRelationship); foreach (var item in deletedRelations) { try { ObjectStateEntry parentEntry = GetEntityEntryFromRelation(item, 0); ObjectStateEntry childEntry; IAuditable <TKey> parent = parentEntry.Entity as IAuditable <TKey>; if (parent != null) { IAuditable <TKey> child; // Find representation of the relation System.Data.Entity.Core.Objects.DataClasses.IRelatedEnd relatedEnd = parentEntry.RelationshipManager.GetAllRelatedEnds().First(r => r.RelationshipSet == item.EntitySet); childEntry = GetEntityEntryFromRelation(item, 1); child = childEntry.Entity as IAuditable <TKey>; if (child != null) { try { TAuditProperty auditProperty = GetAuditProperty(relatedEnd); TAuditItem auditItem = new TAuditItem() { Audit = this.currentAudit, AuditProperty = auditProperty, Entity1 = parent, Entity2 = child, OperationType = "-", OldValue = string.Empty, NewValue = string.Empty }; this.currentAuditItems.Add(auditItem); } catch (Exception ex) { // TODO: log the error? System.Diagnostics.Debug.WriteLine("Error auditing a delete relation: " + ex.Message); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error auditing remove relation: " + ex.Message); } } }