2016-07-20 18 views
0

Mein Block von Code:client.GetServer() veraltet, wie kann ich es reparieren?

public StudentsUnitOfWork() 
     { 
      var connectionString = ConfigurationManager.AppSettings["MongoDBConectionString"]; 
      var client = new MongoClient(connectionString); 
      var server = client.GetServer(); 
      var databaseName = ConfigurationManager.AppSettings["MongoDBDatabaseName"]; 
      _database = server.GetDatabase(databaseName); 
     } 

VS 2015 Ich bekomme client.GetServer() deprecated , how can I fixed it?

und nachdem ich dieses verwenden ändern: sagen, es

public StudentsUnitOfWork() 
     { 
      var connectionString = ConfigurationManager.AppSettings["MongoDBConectionString"]; 
      var client = new MongoClient(connectionString); 
      // var server = client.GetServer(); 
      var databaseName = ConfigurationManager.AppSettings["MongoDBDatabaseName"]; 
      _database = client.GetDatabase(databaseName); 
     } 

, kann nicht implizit konvertieren Typ ..

Antwort

1

Dies ist mein gesamter Zusammenhang und es funktioniert gut für mich:

public class MyContext 
{ 
    public const string COLLECTION_NAME = "CollectionName"; 

    private static readonly IMongoClient _client; 
    private static readonly IMongoDatabase _database; 
    static MyContext() 
    { 
     var connectionString = ConfigurationManager.AppSettings["MongoDBConectionString"]; 
     _client = new MongoClient(connectionString); 
     var databaseName = ConfigurationManager.AppSettings["MongoDBDatabaseName"]; 
     _database = _client.GetDatabase(databaseName); 
    } 

    public IMongoClient Client 
    { 
     get { return _client; } 
    } 

    public IMongoCollection<TDocument> DocumentType => 
     _database.GetCollection<TDocument>(COLLECTION_NAME); 
} 
+0

Vielen Dank für den Code – kn3l