private void CreateAndSaveMediaXml(XElement xml, int id, UmbracoDatabase db) { var poco = new ContentXmlDto { NodeId = id, Xml = xml.ToString(SaveOptions.None) }; var exists = db.FirstOrDefault <ContentXmlDto>("WHERE nodeId = @Id", new { Id = id }) != null; int result = exists ? db.Update(poco) : Convert.ToInt32(db.Insert(poco)); }
protected override void PersistNewItem(ContentXmlEntity <TContent> entity) { if (entity.Content.HasIdentity == false) { throw new InvalidOperationException("Cannot insert or update an xml entry for a content item that has no identity"); } var poco = new ContentXmlDto { NodeId = entity.Id, Xml = entity.Xml.ToDataString() }; //We need to do a special InsertOrUpdate here because we know that the ContentXmlDto table has a 1:1 relation // with the content table and a record may or may not exist so the // unique constraint which can be violated if 2+ threads try to execute the same insert sql at the same time. Database.InsertOrUpdate(poco); }
/// <summary> /// Saves a single <see cref="IMedia"/> object /// </summary> /// <param name="media">The <see cref="IMedia"/> to save</param> /// <param name="userId">Id of the User saving the Content</param> /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events.</param> public void Save(IMedia media, int userId = 0, bool raiseEvents = true) { if (raiseEvents) { if (Saving.IsRaisedEventCancelled(new SaveEventArgs <IMedia>(media), this)) { return; } } using (new WriteLock(Locker)) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateMediaRepository(uow)) { media.CreatorId = userId; repository.AddOrUpdate(media); uow.Commit(); var xml = media.ToXml(); var poco = new ContentXmlDto { NodeId = media.Id, Xml = xml.ToString(SaveOptions.None) }; var exists = uow.Database.FirstOrDefault <ContentXmlDto>("WHERE nodeId = @Id", new { Id = media.Id }) != null; int result = exists ? uow.Database.Update(poco) : Convert.ToInt32(uow.Database.Insert(poco)); } if (raiseEvents) { Saved.RaiseEvent(new SaveEventArgs <IMedia>(media, false), this); } Audit.Add(AuditTypes.Save, "Save Media performed by user", media.CreatorId, media.Id); } }