public virtual void SaveVersioned(TEntity entity) { ++entity.Version; try { var query = Query.And( Query.EQ("_id", BsonValue.Create(entity.Id)), Query.EQ("Version", BsonValue.Create(entity.Version - 1))); var update = MDBUpdate.Replace(entity); Collection.Update(query, update); //LastErrorResponse response = MDb.LastError(); //// If it fails then "updateExisting" won't be available //if (response.Code == (int)ErrorCodes.DuplicateKeyOnUpdate) // throw new DuplicateKeyException(CollectionName, response.Error); //if (!(bool)response["updatedExisting"]) //{ // TEntity existingEntity = Collection.FindOne(new { _id = entity.Id }); // if (existingEntity == null) // throw new MissingResourceException(string.Format("Could not update non-existing {0} with Id {1}.", typeof(TEntity), entity.Id)); // else // throw new VersionConflictException(entity.Id.ToString(), typeof(TEntity), entity.Version); //} } catch (Exception) { --entity.Version; throw; } }
// Add new or update existing public virtual void Put(TEntity entity) { Condition.Requires(entity, "entity").IsNotNull(); var query = Query.EQ("_id", BsonValue.Create(entity.Id)); var update = MDBUpdate.Replace(entity); Collection.Update(query, update); //(new { _id = entity.Id }, entity, false, true); //LastErrorResponse response = MDb.LastError(); //if (response.Code == (int)ErrorCodes.DuplicateKeyOnAdd || response.Code == (int)ErrorCodes.DuplicateKeyOnUpdate) // throw new DuplicateKeyException(CollectionName, response.Error); }
public void SaveOrUpdate(TEntity entity) { var idValue = GetEntityId(entity); if (idValue != 0) { var update = MongoDBUpdate.Replace(entity); var result = Collection.Update( Query.EQ("_id", idValue), update, UpdateFlags.Upsert, WriteConcern.Acknowledged); } else { Save(entity); } }