Reputation: 41
I just started playing with MongoDB and official c# driver for it. 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. Is it possible to achieve it with latest standard c# driver? 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() };
}
Upvotes: 3
Views: 1955
Reputation: 41
Have a look at this project in GitHub.
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.
Upvotes: 1
Reputation: 12187
You could declare your Comments property as List<MongoDBRef> and handle the relationship yourself but there is no automatic support for that.
Upvotes: 0