public void RemoveTags(string photoId, Tag[] tags) { var context = new PhotoAlbumDataContext(); foreach (var tag in tags) { var photoTag = new PhotoTagRow(photoId, tag.Name); context.AttachTo(PhotoAlbumDataContext.PhotoTagTable, photoTag, "*"); context.DeleteObject(photoTag); } try { // continue trying to delete, even if not found context.SaveChanges(SaveChangesOptions.ContinueOnError); } catch (DataServiceRequestException ex) { foreach (var resp in ex.Response) { // to be more robust, we will ignore 404 errors when // the entity might have already been deleted (due to an // incomplete operation earliet). if (resp.StatusCode != (int)HttpStatusCode.NotFound && resp.StatusCode != (int)HttpStatusCode.OK) { throw; } } } }
public void CreateTags(string photoId, Tag[] tags) { var context = new PhotoAlbumDataContext(); foreach (var tag in tags) { // add the tag and associate to a picture for later searching context.AddObject(PhotoAlbumDataContext.TagTable, new TagRow(tag)); context.AddObject(PhotoAlbumDataContext.PhotoTagTable, new PhotoTagRow(photoId, tag.Name)); } try { // we want to continue - even if conflict is detected... context.SaveChanges(SaveChangesOptions.ContinueOnError); } catch (DataServiceRequestException ex) { foreach (var resp in ex.Response) { // we might get a conflict here, which is ok (tag exists) // the alternative is to query everytime to see if the tag // exists, which is less efficient that just trying and // handling exception. if (resp.StatusCode != (int)HttpStatusCode.Conflict && resp.StatusCode != (int)HttpStatusCode.Created) { throw; } } } }