public async Task <bool> Update(Processor.Model.System model) { var dto = model.ToDomainModel(); var original = await ReadDtoSystem(_db, model.Id); var componentsToUnlink = original.Component.Where(c => !dto.Component.Select(comp => comp.ComponentId).Contains(c.ComponentId)); var componentEntities = new List <EntityEntry <Component> >(); foreach (var component in componentsToUnlink) { component.SystemId = null; component.System = null; componentEntities.Add(_db.Component.Update(component)); } var entity = _db.System.Update(dto); await _db.SaveChangesAsync(); entity.State = EntityState.Detached; foreach (var componentEntity in componentEntities) { componentEntity.State = EntityState.Detached; } return(true); }
public async Task <int> Create(Processor.Model.System model) { var dto = model.ToDomainModel(); dto.Component = null; var entity = _db.System.Add(dto); await _db.SaveChangesAsync(); foreach (var modelComponent in model.Components) { var dtoComponent = modelComponent.ToDomainModel(); var exists = await _db.Component.AnyAsync(); dtoComponent.SystemId = entity.Entity.SystemId; if (exists) { _db.Component.Update(dtoComponent); } else { await _db.Component.AddAsync(dtoComponent); } await _db.SaveChangesAsync(); } entity.State = EntityState.Detached; return(entity.Entity.SystemId); }