简体   繁体   English

如何在C#mongo驱动程序中设置复数集合名称?

[英]How to set plural collection name in C# mongo driver?

I'm using the MongoDB default C# driver. 我正在使用MongoDB默认的C#驱动程序。 In my code, I have created an entity (collection) with the name of Customer. 在我的代码中,我创建了一个名为Customer的实体(集合)。 Is there a default convention or class attribute that will set the pluralized version of my collection name in MongoDB ('customers') ? 是否存在默认约定或类属性,该属性将在MongoDB(“客户”)中设置集合名称的复数形式?

I haven't found any so I'm using the Humanizer library to pluralize the names. 我还没有找到任何东西,所以我正在使用Humanizer库来对名称进行复数。 I actually go a step further and use the type itself to create a collection: 我实际上更进一步,并使用类型本身创建了一个集合:

var customers = database.GetCollection(typeof(Customer).Name.Pluralize().ToLower());

Instead of using an external library suggested by I3arnon you can use the .NET pluralizing service available in the System.Data.Entity.Design assembly. 可以使用System.Data.Entity.Design程序集中提供的.NET复数服务,而不使用I3arnon建议的外部库。

You can use it like this: 您可以像这样使用它:

 var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
 var plural = serv.Pluralize(typeof(Person).Name);

It will correctly pluralize Person to People or Customer to Customers like in the example above. 它会正确地以复数的人客户客户就像上面的例子。 It is the same mechanism used by Entity Framework pluralization. 它与实体框架多元化所使用的机制相同。

I ran across this same issue, so I wrote a simple wrapper around the .NET MongoDB driver to automate object to collection name mapping. 我遇到了同样的问题,因此我围绕.NET MongoDB驱动程序编写了一个简单的包装程序,以自动执行对象到集合名称的映射。 It uses System.Data.Entity.Design.PluralizationServices under the hood, so it doesn't rely on any external dependencies. 它在后台使用System.Data.Entity.Design.PluralizationServices ,因此它不依赖任何外部依赖项。 It may save you a few lines of code. 它可以为您节省几行代码。

If you're interested: https://github.com/MikePennington/Mongonizer 如果您有兴趣: https : //github.com/MikePennington/Mongonizer

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

相关问题 如何通过 DateTime 的 C# 驱动程序在 Mongo 集合中查找条目? - How to find an entry in a Mongo Collection through C# driver by DateTime? Mongo DB C#驱动程序-如何在集合中通过ID加入? - Mongo db c# driver - how to join by id in collection? 在mono中使用c#驱动比较mongo集合的两个字段 - Comparing two fields of mongo collection using c# driver in mono 使用c#driver从mongo collection获取DateTime - Get DateTime from mongo collection using c# driver 如何在c#驱动程序中为mongo集合中的所有文本字段创建全文搜索? - How do you create a full text search for all text fields in a mongo collection in the c# driver? 在Mongo Server 4.0上的C#驱动程序2.7.0中如何执行db.collection.explain()? - How do you perform db.collection.explain() in C# driver 2.7.0 on Mongo Server 4.0? 如何使用c#驱动程序复制mongo数据库及其所有集合? - How to copy mongo database with all it's collection using c# driver? 使用 mongo C# 驱动程序在嵌入式文档中维护 Id 属性名称 - Maintain Id property name in embedded doc with mongo C# driver 如何将mongo集合强制转换为C#接口 - How to cast mongo collection to interface C# 在 C# 中,使用 Mongo 驱动程序,如何设置“最后修改字段”? - In C#, using the Mongo Driver, how can I set a "last modified field"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM