BSON Serialization in C # Driver for MongoDB

I just started playing with MongoDB and the official C # driver. And I have a little question about object serialization. For example, we have 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 a serialized collection of maps in User, but a collection of comments like DBRef. Is it possible to achieve this with the latest C # standard driver? It would be great to use some attribute, for example:

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

      

+3


source to share


2 answers


Take a look at this project on GitHub.

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



This project basically provides you with two mapping attributes. OneToMany and ManyToOne. Checkout the code and play with the test project.

+1


source


You can declare your comments property as List <MongoDBRef> and handle the relationship yourself, but there is no automatic support for that.



0


source







All Articles