/// <summary> /// Save attribute value /// </summary> /// <typeparam name="TPropType">Property type</typeparam> /// <param name="entity">Entity</param> /// <param name="key">Key</param> /// <param name="value">Value</param> /// <param name="storeId">Store identifier; pass 0 if this attribute will be available for all stores</param> public virtual void SaveAttribute <TPropType>(FullAuditedEntity entity, string key, TPropType value, int storeId = 0) { if (entity == null) { throw new ArgumentNullException("entity"); } if (key == null) { throw new ArgumentNullException("key"); } string keyGroup = entity.GetUnproxiedEntityType().Name; SaveAttribute(entity.Id, keyGroup, key, value, storeId); //var props = GetAttributesForEntity(entity.Id, keyGroup) // .Where(x => x.StoreId == storeId) // .ToList(); //var prop = props.FirstOrDefault(ga => // ga.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)); //should be culture invariant //var valueStr = CommonUtil.To<string>(value); //if (prop != null) //{ // if (string.IsNullOrWhiteSpace(valueStr)) // { // //delete // DeleteAttribute(prop); // } // else // { // //update // prop.Value = valueStr; // UpdateAttribute(prop); // } //} //else //{ // if (!string.IsNullOrWhiteSpace(valueStr)) // { // //insert // prop = new GenericAttribute // { // EntityId = entity.Id, // Key = key, // KeyGroup = keyGroup, // Value = valueStr, // StoreId = storeId, // }; // InsertAttribute(prop); // } //} }
/// <summary> /// Get an attribute of an entity /// </summary> /// <typeparam name="TPropType">Property type</typeparam> /// <param name="entity">Entity</param> /// <param name="key">Key</param> /// <param name="genericAttributeService">GenericAttributeService</param> /// <param name="storeId">Load a value specific for a certain store; pass 0 to load a value shared for all stores</param> /// <returns>Attribute</returns> public static TPropType GetAttribute <TPropType>(this FullAuditedEntity <long> entity, string key, GenericAttributeDomianService genericAttributeService, int storeId = 0) { if (entity == null) { throw new ArgumentNullException("entity"); } string keyGroup = entity.GetUnproxiedEntityType().Name; return(GetAttribute <TPropType>((int)entity.Id, keyGroup, key, genericAttributeService, storeId)); }
/// <summary> /// Save attribute value /// </summary> /// <typeparam name="TPropType">Property type</typeparam> /// <param name="entity">Entity</param> /// <param name="key">Key</param> /// <param name="value">Value</param> /// <param name="storeId">Store identifier; pass 0 if this attribute will be available for all stores</param> public virtual void SaveAttribute <TPropType>(FullAuditedEntity <long> entity, string key, TPropType value, int storeId = 0) { if (entity == null) { throw new ArgumentNullException("entity"); } if (key == null) { throw new ArgumentNullException("key"); } string keyGroup = entity.GetUnproxiedEntityType().Name; SaveAttribute((int)entity.Id, keyGroup, key, value, storeId); }
public override int SaveChanges() { var modifiedEntries = ChangeTracker.Entries() .Where(x => x.Entity is FullAuditedEntity && (x.State == System.Data.Entity.EntityState.Added || x.State == System.Data.Entity.EntityState.Modified || x.State == System.Data.Entity.EntityState.Deleted)); foreach (var entry in modifiedEntries) { FullAuditedEntity entity = entry.Entity as FullAuditedEntity; if (entity != null && Thread.CurrentPrincipal.Identity != null) { string identityId = Thread.CurrentPrincipal.Identity.GetUserId(); DateTime now = DateTime.UtcNow; if (entry.State == System.Data.Entity.EntityState.Added) { entity.CreatedUserId = identityId; entity.CreatedDate = now; } else if (entry.State == System.Data.Entity.EntityState.Deleted) { entity.DeletedUserID = identityId; entity.DeletedDate = now; } else { base.Entry(entity).Property(x => x.CreatedUserId).IsModified = false; base.Entry(entity).Property(x => x.CreatedDate).IsModified = false; } entity.ModifiedUserId = identityId; entity.ModifiedDate = now; } } return(base.SaveChanges()); }
/// <summary> /// Get unproxied entity type /// </summary> /// <remarks> If your Entity Framework context is proxy-enabled, /// the runtime will create a proxy instance of your entities, /// i.e. a dynamically generated class which inherits from your entity class /// and overrides its virtual properties by inserting specific code useful for example /// for tracking changes and lazy loading. /// </remarks> /// <param name="entity"></param> /// <returns></returns> public static Type GetUnproxiedEntityType(this FullAuditedEntity <long> entity) { var userType = ObjectContext.GetObjectType(entity.GetType()); return(userType); }