gpt4 book ai didi

c# - MongoDB 的 C# 驱动程序中的 BSON 序列化

转载 作者:可可西里 更新时间:2023-11-01 09:53:06 34 4
gpt4 key购买 nike

我刚开始使用 MongoDB 和它的官方 c# 驱动程序。我有一个关于对象序列化的小问题。例如我们有一个类:

public class User
{
public string Name;

public List<Comment> Comments = new List<Comment>(){ new Comment() };
public List<Card> Cards = new List<Card>() { new Card() };
}

public class Comment
{
public string Id;
public string Text;
}

public class Card
{
public string Id;
public string Text;
}

我想在 User 中获取序列化的 Cards 集合,但是像 DBRef 这样的 Comments 集合。是否可以使用最新的标准 c# 驱动程序来实现它?使用一些属性真的很酷:

public class User
{
public string Name;

[UseDBRef]
public List<Comment> Comments = new List<Comment>(){ new Comment() };

public List<Card> Cards = new List<Card>() { new Card() };
}

最佳答案

查看 GitHub 中的这个项目。

https://github.com/virajs/MongoDB-Mapping-Attributes.git

该项目主要为您提供两个映射属性。一对多和多对一。检查代码并试用测试项目。

关于c# - MongoDB 的 C# 驱动程序中的 BSON 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10077829/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com