private void ImportLink(MappedLink mappedLink, Entity currentEntity, bool newEntity) { int linkedEntityId = _entityRepository.GetEntityIdByUniqueValue( mappedLink.LinkedUniqueFieldType, mappedLink.LinkedUniqueFieldValue); if (linkedEntityId == 0) { return; } LinkType linkType = _modelsRepository.GetLinkType(mappedLink.LinkType); if (linkType == null) { throw new InvalidOperationException("The link type '{mappedLink.LinkType}' does not exist in the model."); } Entity sourceEntity; Entity targetEntity; if (mappedLink.Direction == LinkDirection.ChildParent) { sourceEntity = new Entity { Id = linkedEntityId }; targetEntity = currentEntity; } else { sourceEntity = currentEntity; targetEntity = new Entity { Id = linkedEntityId }; } // If entity is new, there would be no existing links. // If it is not new, skip adding a link if it already exists. bool linkExists = !newEntity && _entityRepository.LinkAlreadyExists( sourceEntity.Id, targetEntity.Id, null, linkType.Id); if (linkExists) { return; } Link link = new Link { LinkType = linkType, Source = sourceEntity, Target = targetEntity, Index = mappedLink.SortIndex }; _entityRepository.AddLink(link); }