public void AddHashtagsToPhoto(string[] hashtags, int photoId) { foreach (var hashtag in hashtags.Distinct()) { Hashtag ht = _db.Hashtags.Where(h => h.Text == hashtag).FirstOrDefault(); if (ht == null) { ht = new Hashtag() { Text = hashtag }; _db.Hashtags.Add(ht); } if (_db.PhotoHashtags.Find(photoId, ht.Id) == null) { PhotoHashtag ph = new PhotoHashtag() { HashtagId = ht.Id, PhotoId = photoId }; _db.PhotoHashtags.Add(ph); } } _db.SaveChanges(); }
internal static void AddHashtagsToPhoto(ApplicationDbContext db, string[] hashtags, int photoId) { foreach (var hashtag in hashtags.Distinct()) { Hashtag ht = db.Hashtags.Where(h => h.Text == hashtag).FirstOrDefault(); if (ht == null) { ht = new Hashtag() { Text = hashtag }; db.Hashtags.Add(ht); } if (db.PhotoHashtags.Find(photoId, ht.Id) == null) { PhotoHashtag ph = new PhotoHashtag() { HashtagId = ht.Id, PhotoId = photoId }; db.PhotoHashtags.Add(ph); } } }