簡體   English   中英

MongoDb更新錯誤:超過最大序列化深度

[英]MongoDb Update error: Maximum serialization depth exceeded

我是Mongodb的新手,所以如果我的問題很簡單,請執行。 我在ASP.net MVC項目中使用Mongodb C#驅動程序。 但是,在執行MongoDb C#更新操作時,我遇到了此錯誤

超過最大序列化深度(被序列化的對象是否具有循環引用?)。 說明:執行當前Web請求期間發生未處理的異常。 請查看堆棧跟蹤,以獲取有關錯誤及其在代碼中起源的更多信息。

異常詳細信息:MongoDB.Bson.BsonSerializationException:超過最大序列化深度(被序列化的對象是否具有循環引用?)。

這是對象模型類。 是由於下面的行而導致錯誤,該行在我的模型類中?

公共列表OutGoingFriendRequestList {get; 組; }

public class UserProfileViewModel
{
    public ObjectId Id { get; set; }

    public string UserId { get; set; }

    public string Email { get; set; }

    public string DisplayName { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string UserDescription { get; set; }


    public string UserName { get; set; }

    public List<UserProfileViewModel> OutGoingFriendRequestList { get; set; }



  }
}

這是我嘗試執行更新的Controller Action Method

public async Task<ActionResult> AddFriend(UserProfileViewModel model)
{

  //Get the database connection instance and then get the collection
  var _database = SocialNetworkHelper.ReturnMongoDbInstance();

   IMongoCollection<UserProfileViewModel> collection =  

   _database.GetCollection<UserProfileViewModel>("Facebook_Lite");

    //In the next 2 sentences, I am filtering using the userId field
    string userId = User.Identity.GetUserId();

    var filter = Builders<UserProfileViewModel>.Filter.Eq("UserId", User.Identity.GetUserId());

    var result = collection.Find(filter).ToListAsync();
    result.Wait();
    UserProfileViewModel userProfileViewModelInstance = result.Result.ElementAt(0);

    List<UserProfileViewModel> OutgoingFriendRequestList = userProfileViewModelInstance.OutGoingFriendRequestList;

    OutgoingFriendRequestList.Add(friendUserModelInstance);

    userProfileViewModelInstance.OutGoingFriendRequestList = OutgoingFriendRequestList;

    var update = Builders<UserProfileViewModel>.Update.Set("OutGoingFriendRequestList", userProfileViewModelInstance.OutGoingFriendRequestList);

    //I am hitting the exception on the below line
    **var updateResult = await collection.UpdateOneAsync(filter, update);**

        return RedirectToAction("Index", "Home", new { userid = friendUserId });
}

表示像您這樣的模型的樹的方式是通過保留對子/葉子的引用,而不是對象本身。 代替:

public List<UserProfileViewModel> OutGoingFriendRequestList { get; set; }

您應該存儲這些數據庫重新存儲的ID。

public List<ObjectId> OutGoingFriendRequestList { get; set; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM