public AuditEntryState(ObjectContext objectContext, ObjectStateEntry objectStateEntry) { if (objectStateEntry == null) throw new ArgumentNullException("objectStateEntry"); if (objectStateEntry.Entity == null) throw new ArgumentException("The Entity property is null for the specified ObjectStateEntry.", "objectStateEntry"); if (objectContext == null) { throw new ArgumentNullException("objectContext"); } ObjectStateEntry = objectStateEntry; ObjectContext = objectContext; ObjectType = ObjectContext.GetObjectType(objectStateEntry.Entity.GetType()); Entity = objectStateEntry.Entity; EntityType = objectContext.MetadataWorkspace.GetItems(DataSpace.OSpace).OfType<EntityType>().FirstOrDefault(x => x.Name == ObjectType.Name); EntityAccessor = TypeAccessor.GetAccessor(ObjectType); AuditEntity = new AuditEntity(objectStateEntry.Entity) { Action = GetAction(objectStateEntry), }; }
public virtual bool IsTargetEntity(ObjectStateEntry item) { return item.State != EntityState.Detached && TargetType.IsInstanceOfType(item.Entity); }
private IDbObjectStateEntry ToDbObjectStateEntry(ObjectStateEntry objectStateEntry) { IDbObjectStateEntry dbObjectStateEntry = null; if (objectStateEntry != null) { dbObjectStateEntry = new DbObjectStateEntry { Entity = objectStateEntry.Entity, EntityKey = objectStateEntry.EntityKey, State = this.ToDbObjectState(objectStateEntry.State) }; } return dbObjectStateEntry; }
private static AuditAction GetAction(ObjectStateEntry entity) { switch (entity.State) { case EntityState.Added: return AuditAction.Added; case EntityState.Deleted: return AuditAction.Deleted; default: return AuditAction.Modified; } }
internal ObjectStateEntryDbDataRecord(RelationshipEntry cacheEntry) { Contract.Requires(cacheEntry != null); Debug.Assert(!cacheEntry.IsKeyEntry, "Cannot create an ObjectStateEntryDbDataRecord for a key entry"); switch (cacheEntry.State) { case EntityState.Unchanged: case EntityState.Modified: case EntityState.Deleted: _cacheEntry = cacheEntry; break; default: Debug.Assert(false, "A DbDataRecord cannot be created for an entity object that is in an added or detached state."); break; } }
/// <summary> /// Returns the changes for a property as ValuePair objects. /// We can have more than one change for a single property if it is a complex type /// (A complex type is another class with its own properties that is mapped not as a /// foreign key but as columns of the same table.) /// </summary> public static IEnumerable<IValuePair> Get(ObjectStateEntry entry, string propertyName) { var state = entry.State; switch (state) { case EntityState.Added: return Get(null, () => entry.CurrentValues[propertyName], propertyName, state); case EntityState.Deleted: return Get(() => entry.OriginalValues[propertyName], null, propertyName, state); case EntityState.Modified: return Get(() => entry.OriginalValues[propertyName], () => entry.CurrentValues[propertyName], propertyName, state); default: throw new NotImplementedException(string.Format("Unable to deal with ObjectStateEntry in '{0}' state", state)); } }
public ChangeEntry(ObjectStateEntry f) { Entity = f.Entity; State = f.State; OriginalValues = new Dictionary<string, object>(); EntityType = Entity.GetType().GetEntityType(); if (State != EntityState.Modified) return; var d = OriginalValues; foreach (var item in f.GetModifiedProperties()) { d[item] = f.OriginalValues[item]; } OriginalValues = d; }
internal ObjectStateEntryDbDataRecord(EntityEntry cacheEntry, StateManagerTypeMetadata metadata, object userObject) { Contract.Requires(cacheEntry != null); Contract.Requires(userObject != null); Contract.Requires(metadata != null); Debug.Assert(!cacheEntry.IsKeyEntry, "Cannot create an ObjectStateEntryDbDataRecord for a key entry"); switch (cacheEntry.State) { case EntityState.Unchanged: case EntityState.Modified: case EntityState.Deleted: _cacheEntry = cacheEntry; _userObject = userObject; _metadata = metadata; break; default: Debug.Assert(false, "A DbDataRecord cannot be created for an entity object that is in an added or detached state."); break; } }
public AuditEntryState(ObjectStateEntry objectStateEntry) { if (objectStateEntry == null) throw new ArgumentNullException("objectStateEntry"); if (objectStateEntry.Entity == null) throw new ArgumentException("The Entity property is null for the specified ObjectStateEntry.", "objectStateEntry"); ObjectStateEntry = objectStateEntry; Entity = objectStateEntry.Entity; EntityType = objectStateEntry.EntitySet.ElementType as EntityType; Type entityType = objectStateEntry.Entity.GetType(); entityType = ObjectContext.GetObjectType(entityType); ObjectType = entityType; EntityAccessor = TypeAccessor.GetAccessor(entityType); AuditEntity = new AuditEntity(objectStateEntry.Entity) { Action = GetAction(objectStateEntry), }; }
/// <summary> /// Get the records of the object that will /// be inserted to the table in the form of a XML file /// </summary> /// <param name="entry">ObjectStateEntry that carries the /// original and current values of the table</param> /// <returns>XML string of the respective table record</returns> private string GetEntryValueInString(System.Data.Entity.Core.Objects.ObjectStateEntry entry) { try { string xml = "<?xml version='1.0' encoding='utf-16'?> <" + (entry.EntitySet).Name + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"; var currentValues = (entry.State == System.Data.Entity.EntityState.Deleted) ? entry.OriginalValues : entry.CurrentValues; for (int i = 0; i < currentValues.FieldCount; i++) { //<StudentName type="int">Nirosh</StudentName xml += "<" + currentValues.GetName(i) + " type='" + currentValues.GetFieldType(i) + "'>" + currentValues.GetValue(i) + "</" + currentValues.GetName(i) + ">"; } xml += "</" + (entry.EntitySet).Name + ">"; return(xml); } catch (Exception e) { return("<?xml version='1.0' encoding='utf-16'?> <Error xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" + e.Message + "</Error>"); } }
private IModelObject GetOriginalVersion(KcsarContext context, ObjectStateEntry entry) { object original; if (entry.EntityKey == null || !(entry.Entity is IModelObject)) { // We don't know how to report objects that aren't IModelObjects return null; } // Try to get the original version of the deleted object. EntityKey key = new EntityKey(entry.EntityKey.EntityContainerName + "." + entry.EntityKey.EntitySetName, "Id", ((IModelObject)entry.Entity).Id); ((IObjectContextAdapter)context).ObjectContext.TryGetObjectByKey(key, out original); foreach (var property in original.GetType().GetProperties()) { foreach (ReportedReferenceAttribute attrib in property.GetCustomAttributes(typeof(ReportedReferenceAttribute), true)) { var reference = context.Entry(original).Reference(property.Name); if (!reference.IsLoaded) reference.Load(); } } // Now that we have the object before it (and its associations) was deleted, we can report on what it was... return original as IModelObject; }
internal DbUpdatableDataRecord(ObjectStateEntry cacheEntry) : this(cacheEntry, null, null) { }
internal DbUpdatableDataRecord(ObjectStateEntry cacheEntry, StateManagerTypeMetadata metadata, object userObject) { _cacheEntry = cacheEntry; _userObject = userObject; _metadata = metadata; }
private void AuditChange(ObjectStateEntry entry) { var obj = entry.Entity as IModelObject; if (obj == null) return; var audit = new AuditLog { Action = entry.State.ToString(), Changed = DateTime.Now, ObjectId = obj.Id, User = Thread.CurrentPrincipal.Identity.Name, Collection = entry.EntitySet.Name }; switch (entry.State) { case EntityState.Added: audit.Comment = obj.GetReportHtml(); break; case EntityState.Modified: string report = string.Format("<b>{0}</b><br/>", entry.Entity); foreach (var prop in entry.GetModifiedProperties()) { var displayFormat = entry.Entity.GetType().GetProperty(prop).GetCustomAttribute<DisplayFormatAttribute>(); string format = displayFormat == null ? "{0}" : displayFormat.DataFormatString; report += string.Format( "{0}: {1} => {2}<br/>", prop, string.Format(format, entry.OriginalValues[prop]), string.Format(format, entry.CurrentValues[prop])); } audit.Comment = report; break; case EntityState.Deleted: object original; this.ComparisonContext.TryGetObjectByKey(entry.EntityKey, out original); audit.Comment = ((IModelObject)original).GetReportHtml(); break; default: throw new NotImplementedException("Unhandled state" + entry.State.ToString()); } this.AuditLog.Add(audit); }
public StateEntryAdapter(ObjectStateEntry stateEntry) { Contract.Requires(stateEntry != null); _stateEntry = stateEntry; }
public StateEntryAdapter(ObjectStateEntry stateEntry) { DebugCheck.NotNull(stateEntry); _stateEntry = stateEntry; }
/// <summary> /// 初始化版本号 /// </summary> private void InitVersion( ObjectStateEntry entry ) { var entity = entry.Entity as IAggregateRoot; if ( entity == null ) return; entity.Version = Encoding.UTF8.GetBytes( Guid.NewGuid().ToString() ); }
/// <summary> /// Convert an ObjectStateEntry object to a string representation /// </summary> /// <param name="entry">The given ObjectStateEntry</param> /// <returns>The string representation</returns> private static string ObjectStateEntryToString(ObjectStateEntry entry) { var builder = new StringBuilder(); builder.AppendFormat("\n- <b>{0} ", entry.State); if (entry.EntityKey == null) { if (entry.EntitySet == null) builder.Append("Entity : null </b>[null]"); else builder.AppendFormat("EntitySet : {0}</b>", entry.EntitySet.Name); } else { builder.AppendFormat("Entity : {0} </b>", entry.EntityKey.EntitySetName); if (entry.EntityKey.IsTemporary) { builder.Append("[Temporary]"); } else { foreach (var key in entry.EntityKey.EntityKeyValues) { builder.AppendFormat("[{0} = {1}]", key.Key, ObjectToString(key.Value)); } } } return (builder.ToString()); }
private IDbObjectStateEntry ToEntryState(ObjectStateEntry entry) { if (entry != null) return new DbObjectStateEntry { Entity = entry.Entity, EntityKey = entry.EntityKey, State = ToObjectState(entry.State) }; return null; }