Esempio n. 1
0
        public virtual async Task <TEntity> UpdateAsync(dynamic partialEntity, params object[] keyValues)
        {
            if (partialEntity == null)
            {
                throw new MissingEntityException(
                          $"Cannot update a null {typeof(TEntity).Name}");
            }

            List <string> props = DynamicExtensions.GetProperties(partialEntity);

            if (!props.Contains("SysUser") || partialEntity.SysUser == null)
            {
                partialEntity.SysUser = ScopeProperties.User;
            }

            //retrieve the existing entity
            var existing = await Context.FindAsync <TEntity>(keyValues);

            //copy property values from entity to existing
            DynamicExtensions.Populate <TEntity>(existing, partialEntity);

            Context.Update(existing);
            await Context.SaveChangesAsync();

            return(existing); //updated entity
        }
Esempio n. 2
0
        public override async Task <TEntity> UpdateAsync(dynamic partialEntity, params object[] keyValues)
        {
            if (partialEntity == null)
            {
                throw new MissingEntityException(
                          $"Cannot update a null {typeof(TEntity).Name}");
            }

            List <string> props = DynamicExtensions.GetProperties(partialEntity);

            if (!props.Contains("SysUser") || partialEntity.SysUser == null)
            {
                partialEntity.SysUser = ScopeProperties.User;
            }
            if (!props.Contains("SysStart") || partialEntity.SysStart == default(DateTime))
            {
                partialEntity.SysStart = DateTime.Now;
            }
            if (!props.Contains("SysEnd") || partialEntity.SysEnd == default(DateTime))
            {
                partialEntity.SysEnd = DateTime.MaxValue;
            }

            //retrieve the existing entity
            var existing = await Context.FindAsync <TEntity>(keyValues);

            if (WriteUpdate(partialEntity, existing))
            {
                WriteToHistory(existing);
            }

            //copy property values from entity to existing
            DynamicExtensions.Populate <TEntity>(existing, partialEntity);

            Context.Update(existing);
            await Context.SaveChangesAsync();

            return(existing); //updated entity
        }