/// <summary> /// Fill the shadow properties during update considering the tracker /// </summary> private void UpdateShadowProperties(T entity) { DbContext.WriteDebug("Update Shadow Properties"); var entry = DbContext.Entry(entity); var propertyNames = entry.Metadata .GetProperties() .Select(p => p.Name); var props = propertyNames.Select(p => entry.Property(p)) .Where(e => e.IsModified); var entityType = typeof(T); foreach (var prop in props) { if (prop.Metadata .IsShadowProperty()) { continue; } var searchInfo = EntityTypeBuilderExtensions.GetShadowField(entityType, prop); var value = prop.CurrentValue; if (searchInfo != null && value != null) { DbContext.Entry(entity) .Property(searchInfo.ColumnName) .CurrentValue = value.ToString() .ToSearchable(searchInfo.SearchableType); } } }
public void Update(T entity) { DbContext.Entry(entity) .State = EntityState.Modified; if (EntityTypeBuilderExtensions.HasShadowField(typeof(T))) { UpdateShadowProperties(entity); } }
public void Add(T entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } // fill shadow property if (EntityTypeBuilderExtensions.HasShadowField(typeof(T))) { InsertShadowProperties(entity); } _dbSet.Add(entity); }
public void Add(IEnumerable <T> entities) { var enumerable = entities.ToList(); if (entities == null || !enumerable.Any()) { throw new ArgumentNullException(nameof(entities)); } // fill shadow property if (EntityTypeBuilderExtensions.HasShadowField(typeof(T))) { foreach (var entity in enumerable) { InsertShadowProperties(entity); } } _dbSet.AddRange(enumerable); }
/// <summary> /// Fill the shadow properties during insert /// </summary> private void InsertShadowProperties(T entity) { DbContext.WriteDebug("Insert Shadow Properties"); var props = entity.GetType() .GetProperties(); var entityType = typeof(T); foreach (var prop in props) { var searchInfo = EntityTypeBuilderExtensions.GetShadowField(entityType, prop); var value = prop.GetValue(entity); if (searchInfo != null && value != null) { DbContext.WriteDebug($"Shadow Properties [{searchInfo.ColumnName}] = {value}"); DbContext.Entry(entity) .Property(searchInfo.ColumnName) .CurrentValue = value.ToString() .ToSearchable(searchInfo.SearchableType); } } }