private void AddTopicsToPublications(string[] topics)
 {
     int publicationId = periodicalContext.Publications.OrderByDescending(x => x.PublicationId).First().PublicationId;
     List<TopicToPublication> topicsToPublication = new List<TopicToPublication>();
     foreach (string item in topics)
     {
         TopicToPublication newTopicToPublication = new TopicToPublication();
         newTopicToPublication.PublicationId = publicationId;
         newTopicToPublication.TopicId = periodicalContext.Topics.Where(x => x.TopicName == item).Select(x => x.Id).FirstOrDefault();
         periodicalContext.TopicsToPublications.Add(newTopicToPublication);
     }
     SaveChanges();
 }
 private void UpdateTopicsToPublications(string[] topics, int publicationId)
 {
     List<int> oldTopicsId = periodicalContext.TopicsToPublications.Where(x => x.PublicationId == publicationId).Select(x => x.TopicId).ToList();
     List<int> newTopicsId = new List<int>();
     foreach(string item in topics)
     {
         int id = periodicalContext.Topics.Where(x => x.TopicName == item).Select(x => x.Id).FirstOrDefault();
         newTopicsId.Add(id);
     }
     //delete topics
     foreach(int id in oldTopicsId)
     {
         if (!newTopicsId.Contains(id))
         {
             TopicToPublication forDelete = periodicalContext.TopicsToPublications.Where(x => x.TopicId == id && x.PublicationId == publicationId).Select(x => x).FirstOrDefault();
             periodicalContext.TopicsToPublications.Remove(forDelete);
         }
     }
     //add new
     foreach(int id in newTopicsId)
     {
         if (!oldTopicsId.Contains(id))
         {
             TopicToPublication forAdd = new TopicToPublication();
             forAdd.PublicationId = publicationId;
             forAdd.TopicId = id;
             periodicalContext.TopicsToPublications.Add(forAdd);
         }
     }
     SaveChanges();
 }