[英]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.