public async Task <int> Create(ParsingRules model) { var dto = model.ToDomainModel(); var component = dto.Components.First(); dto.Components = null; var entity = _db.ParsingRules.Add(dto); await _db.SaveChangesAsync(); entity.State = EntityState.Detached; component.ParsingRulesId = entity.Entity.ParsingRulesId; var componentEntity = _db.Component.Update(component); await _db.SaveChangesAsync(); componentEntity.State = EntityState.Detached; return(entity.Entity.ParsingRulesId); }
public async Task <bool> Update(ParsingRules model) { var dto = model.ToDomainModel(); var original = await ReadDtoParsingRules(_db, model.Id); var originalCustomAttributeIds = original.CustomAttributeRuleParsingRules.Select(c => c.GeneralRuleId).ToList(); var currentCustomAttributeIds = dto.CustomAttributeRuleParsingRules.Select(c => c.GeneralRuleId).ToList(); // all in original except those that were present in original and are present in current var toDelete = originalCustomAttributeIds.Except(originalCustomAttributeIds.Intersect(currentCustomAttributeIds)); // all that are in current but were not in original var toCreate = currentCustomAttributeIds.Except(originalCustomAttributeIds); foreach (var customAttributeRuleId in toDelete) { var delete = new CustomAttributeRuleParsingRules { ParsingRulesId = model.Id, GeneralRuleId = customAttributeRuleId }; _db.CustomAttributeRuleParsingRules.Remove(delete); } foreach (var customAttributeCreateId in toCreate) { var create = new CustomAttributeRuleParsingRules { ParsingRulesId = model.Id, GeneralRuleId = customAttributeCreateId }; _db.CustomAttributeRuleParsingRules.Add(create); } await _db.SaveChangesAsync(); var entity = _db.ParsingRules.Update(dto); await _db.SaveChangesAsync(); entity.State = EntityState.Detached; return(true); }