2016-07-23 71 views
1

使用C#蒙戈驅動程序版本1 Repository更新文件V2從驅動器V1遷移

 /// <summary> 
     /// Generic update method to update record on the basis of id 
     /// </summary> 
     /// <param name="queryExpression"></param> 
     /// <param name="id"></param> 
     /// <param name="entity"></param> 
     public void Update(Expression<Func<T, string>> queryExpression, string id, T entity) 
     { 
      var query = Query<T>.EQ(queryExpression, id); 
      _collection.Update(query, Update<T>.Replace(entity)); 
     } 

我的代碼,我修改代碼,C#驅動程序版本2

/// <summary> 
    /// Generic update method to update record on the basis of id 


    /// </summary> 
    /// <param name="queryExpression"></param> 
    /// <param name="id"></param> 
    /// <param name="entity"></param> 
    public void Update(Expression<Func<T, string>> queryExpression, string id, T entity) 
    { 
     // var query = Query<T>.EQ(queryExpression, id); 
     //_collection.Update(query, Update<T>.Replace(entity)); 


     var query = Builders<T>.Filter.Eq(queryExpression, id); 
     var update = Builders<T>.Update.Set(queryExpression, id); 
     _collection.UpdateOneAsync(query, update); ; 

    } 

我通過調用( controller):

public void Update(PostModel post) 
     { 
      _postRepo.Posts.Update(s => s.Id, post.Id, post); 
     } 

我沒有收到文檔update.Do you know wh at是我的遷移代碼的問題。

感謝

回答

1

你叫不await_collection.UpdateOneAsync(query, update);異步方法,它是不是你的問題的根本原因,但你有沒有適當的例外在這種情況下處理。
無論await或使用相應的同步版本UpdateOne


你可能也更願意使用ReplaceOne作爲與V1司機最初的版本也做了一個整個文件替換。以下應該適合您的需求(未經測試)

... 
var query = Builders<T>.Filter.Eq(queryExpression, id); 
_collection.ReplaceOne(query, entity); 
... 
+0

感謝您的幫助,它的工作原理。 – kn3l

+0

我可以使用async .. await +異步方法嗎?摻在一起? – kn3l