@@ -54,7 +54,6 @@ public class MongoConnectionStringBuilder : DbConnectionStringBuilder
5454 { "readpreference" , "readPreference" } ,
5555 { "readpreferencetags" , "readPreferenceTags" } ,
5656 { "replicaset" , "replicaSet" } ,
57- { "safe" , "safe" } ,
5857 { "secondaryacceptablelatency" , "secondaryAcceptableLatency" } ,
5958 { "secondaryacceptablelatencyms" , "secondaryAcceptableLatency" } ,
6059 { "server" , "server" } ,
@@ -91,7 +90,6 @@ public class MongoConnectionStringBuilder : DbConnectionStringBuilder
9190 private string _password ;
9291 private ReadPreference _readPreference ;
9392 private string _replicaSetName ;
94- private bool ? _safe ;
9593 private TimeSpan _secondaryAcceptableLatency ;
9694 private IEnumerable < MongoServerAddress > _servers ;
9795 private bool ? _slaveOk ;
@@ -376,27 +374,6 @@ public string ReplicaSetName
376374 }
377375 }
378376
379- /// <summary>
380- /// Gets or sets the safe value.
381- /// </summary>
382- [ Obsolete ( "Use W=0 or W=1 instead." ) ]
383- public bool ? Safe
384- {
385- get { return _safe ; }
386- set
387- {
388- if ( value != null )
389- {
390- if ( ! value . Value && AnyWriteConcernSettingsAreSet ( ) )
391- {
392- throw new InvalidOperationException ( "Safe cannot be set to false if any other write concern values have been set." ) ;
393- }
394- }
395- _safe = value ;
396- base [ "safe" ] = ( value == null ) ? null : XmlConvert . ToString ( value . Value ) ;
397- }
398- }
399-
400377 /// <summary>
401378 /// Gets or sets the SafeMode to use.
402379 /// </summary>
@@ -405,7 +382,7 @@ public SafeMode SafeMode
405382 {
406383 get
407384 {
408- if ( _safe != null || AnyWriteConcernSettingsAreSet ( ) )
385+ if ( AnyWriteConcernSettingsAreSet ( ) )
409386 {
410387#pragma warning disable 618
411388 return new SafeMode ( GetWriteConcern ( false ) ) ;
@@ -418,23 +395,20 @@ public SafeMode SafeMode
418395 }
419396 set
420397 {
421- Safe = null ;
422- FSync = null ;
423- Journal = null ;
424- W = null ;
425- WTimeout = null ;
426-
427- if ( value != null )
398+ if ( value == null )
428399 {
429- Safe = value . Enabled ;
430- if ( value . Enabled )
431- {
432- var writeConcern = value . WriteConcern ;
433- if ( writeConcern . FSync != null ) { FSync = writeConcern . FSync . Value ; }
434- if ( writeConcern . Journal != null ) { Journal = writeConcern . Journal . Value ; }
435- if ( writeConcern . W != null ) { W = writeConcern . W ; }
436- if ( writeConcern . WTimeout != null ) { WTimeout = writeConcern . WTimeout . Value ; }
437- }
400+ FSync = null ;
401+ Journal = null ;
402+ W = null ;
403+ WTimeout = null ;
404+ }
405+ else
406+ {
407+ var writeConcern = value . WriteConcern ;
408+ FSync = writeConcern . FSync ;
409+ Journal = writeConcern . Journal ;
410+ W = writeConcern . W ?? ( writeConcern . Enabled ? 1 : 0 ) ;
411+ WTimeout = writeConcern . WTimeout ;
438412 }
439413 }
440414 }
@@ -746,9 +720,27 @@ public override object this[string keyword]
746720 ReplicaSetName = ( string ) value ;
747721 break ;
748722 case "safe" :
749- #pragma warning disable 618
750- Safe = Convert . ToBoolean ( value ) ;
751- #pragma warning restore
723+ var safe = Convert . ToBoolean ( value ) ;
724+ if ( _w == null )
725+ {
726+ W = safe ? 1 : 0 ;
727+ }
728+ else
729+ {
730+ if ( safe )
731+ {
732+ // don't overwrite existing W value unless it's 0
733+ var wCount = _w as WriteConcern . WCount ;
734+ if ( wCount != null && wCount . Value == 0 )
735+ {
736+ W = 1 ;
737+ }
738+ }
739+ else
740+ {
741+ W = 0 ;
742+ }
743+ }
752744 break ;
753745 case "secondaryacceptablelatency" :
754746 case "secondaryacceptablelatencyms" :
@@ -838,7 +830,7 @@ public override bool ContainsKey(string keyword)
838830 /// <returns>A WriteConcern.</returns>
839831 public WriteConcern GetWriteConcern ( bool enabledDefault )
840832 {
841- return new WriteConcern ( _safe . HasValue ? _safe . Value : enabledDefault )
833+ return new WriteConcern ( enabledDefault )
842834 {
843835 FSync = _fsync ,
844836 Journal = _journal ,
@@ -945,7 +937,6 @@ private void ResetValues()
945937 _password = null ;
946938 _readPreference = null ;
947939 _replicaSetName = null ;
948- _safe = null ;
949940 _secondaryAcceptableLatency = MongoDefaults . SecondaryAcceptableLatency ;
950941 _servers = null ;
951942 _slaveOk = null ;
0 commit comments