C# MongoDb更新错误:超过最大序列化深度
我是Mongodb的新手,所以如果我的问题是基本的,请执行。我在ASP.net MVC项目中使用Mongodb C#驱动程序。但是,在执行MongoDb C#update操作时,我遇到了这个错误 超过最大序列化深度(正在序列化的对象是否具有循环引用?)。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:MongoDB.Bson.BsonSerializationException:超出最大序列化深度(正在序列化的对象是否具有循环引用?)。 下面是对象模型类。错误是由于我的模型类中的下面一行引起的吗 公开列表OutGoingFriendRequestList{get;set;}C# MongoDb更新错误:超过最大序列化深度,c#,mongodb,serialization,C#,Mongodb,Serialization,我是Mongodb的新手,所以如果我的问题是基本的,请执行。我在ASP.net MVC项目中使用Mongodb C#驱动程序。但是,在执行MongoDb C#update操作时,我遇到了这个错误 超过最大序列化深度(正在序列化的对象是否具有循环引用?)。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:MongoDB.Bson.BsonSerializationException:超出最大序列化深度(正在序列化的对象是否
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; }
}
}
公共类UserProfileViewModel
{
公共对象Id{get;set;}
公共字符串用户标识{get;set;}
公共字符串电子邮件{get;set;}
公共字符串DisplayName{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串UserDescription{get;set;}
公共字符串用户名{get;set;}
公共列表OutGoingFriendRequestList{get;set;}
}
}
这是我尝试进行更新的控制器操作方法
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 });
}
公共异步任务AddFriend(UserProfileViewModel模型)
{
//获取数据库连接实例,然后获取集合
var_database=SocialNetworkHelper.ReturnMongoDbInstance();
IMongoCollection集合=
_数据库.GetCollection(“Facebook_Lite”);
//在接下来的两句话中,我将使用userId字段进行过滤
字符串userId=User.Identity.GetUserId();
var filter=Builders.filter.Eq(“UserId”,User.Identity.GetUserId());
var result=collection.Find(filter.ToListAsync();
result.Wait();
UserProfileViewModel userProfileViewModelInstance=result.result.ElementAt(0);
List OutgoingFriendRequestList=userProfileViewModelInstance.OutgoingFriendRequestList;
Add(friendUserModelInstance);
userProfileViewModelInstance.OutGoingFriendRequestList=OutGoingFriendRequestList;
var update=Builders.update.Set(“OutGoingFriendRequestList”,userProfileViewModelInstance.OutGoingFriendRequestList);
//我在下面的一行中遇到异常
**var updateResult=await collection.UpdateOneAsync(filter,update)**
返回RedirectToAction(“Index”,“Home”,new{userid=friendUserId});
}
表示类似于您的树模型的方式是通过保存对子对象/叶的引用,而不是对象本身。而不是:
public List OutGoingFriendRequestList{get;set;}
您应该存储这些数据库redords的ID
public List OutGoingFriendRequestList{get;set;}