public static InternalEntityEntry GetInternalEntityEntry(this EntityEntry entityEntry) { var internalEntry = (InternalEntityEntry)entityEntry .GetType() .GetProperty("InternalEntry", BindingFlags.NonPublic | BindingFlags.Instance) .GetValue(entityEntry); return(internalEntry); }
public static EntityEntry CheckInsert(this EntityEntry entity, IPrincipal principal) { var entityPropertys = typeof(ICreationAudited <>).GetProperties(); foreach (var item in entityPropertys) { var entityProperty = entity.GetType().GetProperties().Where(x => x.Name == item.Name).FirstOrDefault(); if (entityProperty != null) { //entityProperty.SetValue(entityProperty.Name,principal.Identity.GetUesrId<>) } } var creationAudited = entity.GetType().GetInterface(/*$"ICreationAudited`1"*/ typeof(ICreationAudited <>).Name); if (creationAudited == null) { return(entity); } entity.CheckICreatedTime(principal); return(entity); }
private static string GetEntityName(EntityEntry e) { string Result = ""; if (e.Entity.GetType().GetTypeInfo() != null && e.Entity.GetType().Namespace == "System.Data.Entity.DynamicProxies") { Result = e.GetType().GetTypeInfo().BaseType.FullName; } else { Result = e.Entity.GetType().FullName; } return(Result); } // GetEntityName
private static IEnumerable <AuditLog> GetAuditLogs(EntityEntry entityEntry, string userName, EntityState entityState) { var returnValue = new List <AuditLog>(); if (entityEntry.GetType().GetCustomAttribute(typeof(DoNotAudit), true) == null) { return(returnValue); } var keyRepresentation = BuildKeyRepresentation(entityEntry, KeySeperator); var auditedPropertyNames = entityEntry.Entity.GetType() .GetRuntimeProperties().Where(p => !p.GetCustomAttributes(typeof(DoNotAudit), true).Any()) .Select(info => info.Name); foreach (var propertyEntry in entityEntry.Metadata.GetProperties() .Where(x => auditedPropertyNames.Contains(x.Name)) .Select(property => entityEntry.Property(property.Name))) { if (entityState == EntityState.Modified) { if (Convert.ToString(propertyEntry.OriginalValue) == Convert.ToString(propertyEntry.CurrentValue)) //Values are the same, don't log { continue; } } returnValue.Add(new AuditLog { KeyNames = keyRepresentation.Key, KeyValues = keyRepresentation.Value, OriginalValue = entityState != EntityState.Added ? Convert.ToString(propertyEntry.OriginalValue) : null, NewValue = entityState == EntityState.Modified || entityState == EntityState.Added ? Convert.ToString(propertyEntry.CurrentValue) : null, ColumnName = propertyEntry.Metadata.Name, EventDateTime = DateTime.Now, EventType = entityState.ToString(), UserName = userName, TableName = entityEntry.Entity.GetType().Name }); } return(returnValue); }
private void CheckAdd(EntityEntry entity) { var creationAudited = entity.GetType().GetInterface(typeof(ICreationAudited <>).Name); if (!creationAudited.IsNull()) { var typeArguments = creationAudited?.GenericTypeArguments[0]; } //if (creationAudited == null) //{ // return entity; //} //var typeArguments = creationAudited?.GenericTypeArguments[0]; //var fullName = typeArguments?.FullName; //if (fullName == typeof(Guid).FullName) //{ // entity = CheckIModificationAudited<Guid>(entity); //} //return entity; }
protected virtual bool HasCreatedTimeProperty(EntityEntry entity) { return(entity.GetType().GetProperty(nameof(ICreationAudited <Guid> .CreatedTime)) != null); }