简体   繁体   English

C#驱动中的BSON序列化为MongoDB

[英]BSON serialization in C# driver for MongoDB

I just started playing with MongoDB and official c# driver for it.我刚开始玩 MongoDB 和官方 c# 驱动程序。 And I have a small question regarding objects serialization.我有一个关于对象序列化的小问题。 For example we have a classes:例如我们有一个类:

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;
}

I want to get serialized Cards collection within User, but Comments collection like DBRef.我想在用户中获得序列化的卡片集合,但像 DBRef 这样的评论集合。 Is it possible to achieve it with latest standard c# driver?是否可以使用最新的标准 c# 驱动程序来实现? It will be really cool to use some attribute like:使用一些属性真的很酷:

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() };
}

Have a look at this project in GitHub.在 GitHub 中查看此项目。

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

This project mainly provide you with two mapping attributes.该项目主要为您提供两个映射属性。 OneToMany and ManyToOne.一对多和多对一。 Checkout the code and play around with the test project.检查代码并试用测试项目。

You could declare your Comments property as List<MongoDBRef> and handle the relationship yourself but there is no automatic support for that.您可以将您的 Comments 属性声明为 List<MongoDBRef> 并自己处理关系,但没有对此的自动支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM