How to have a different name for entries of a mongodb document in C#?

て烟熏妆下的殇ゞ 提交于 2019-12-02 11:35:54

问题


I'm trying to do CRUD operations on documents in MongoDB and C#. I would like to have fixed structured domain entities in C# with long meaningful property names but since the name of each property will be saved in MongoDB for each document, that's not a good idea. That's because property names will be redundantly saved in database and affect the total storage and performance.

The solution I can think of to overcome this problem, is to use different names for properties in C# than in MongoDB which means a mapping between them is needed. One elegant way of doing this is to incorporate C#'s attributes, something like this:

class Book
{
    [BsonProperty("n")]
    public string Name { get; set; }
    [BsonProperty("a")]
    public string Author { get; set; }
}

In this case documents would be saved in database like this:

{ n: "...", a: "..." }

I'm not sure if this is support in MongoDB's C# Driver or not!? I can not find it myself but I was hoping I'm looking in wrong places!


回答1:


I found it at last.

class Book
{
    [BsonElement("n")]
    public string Name { get; set; }
    [BsonElement("a")]
    public string Author { get; set; }
}

And this is the page with details.



来源:https://stackoverflow.com/questions/19331880/how-to-have-a-different-name-for-entries-of-a-mongodb-document-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!