为什么实体框架要使用复数和大写的实体类名称?
为什么实体框架默认将所有类名复数并首字母大写?这样做有什么好处呢?可能是一个愚蠢的问题......但只是出于好奇?
Why does entity framework pluralize all the class names and capitalize the first letters by default? What are the advantages of doing this ? Probably a stupid question...but just out of curiosity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.NET 风格指南规定所有类名都应采用 CamelCase。
我不知道复数,它不是复数单个实体对象,是吗?不过对于收藏来说还是有意义的。
The .NET style guidelines say that all class names should be in CamelCase.
I don't know about pluralizing though, it's not pluralizing individual entity objects, is it? Would make sense for collections, though.
在 .NET 开发类库设计指南,它规定所有类名都应采用 Pascal 大小写,它定义为:
至于复数,我发现当您使用实体框架模型设计器的默认设置时,您会得到相反的结果。它将把复数表名称转换为其单数等效项。例如,使用 Northwind 数据库并使用默认设置我发现设计者会将 Products 表更改为名为
Product
的类,将类别表更改为名为Category
的类,这是有道理的,因为对象的单个实例将。是Product
而不是Products
如果您得到相反的效果,那么我很困惑,请查看这篇文章 - 实体框架 4.0:复数化 作者:Dan Rigsby,这也许解释发生了什么。
In the Capitalization Conventions section of the .NET Design Guidelines for Developing Class Libraries, it states that all class names should be in Pascal case, which it defines as:
As for pluralization, I find you get the opposite when you use the default settings for the Entity Framework model designer. It will convert plural Table names to their singular equivalent. For instance, using the Northwind database and using the default settings I find that the designer will change the Products table to a class called
Product
and Categories table to a class calledCategory
. This makes sense since a single instance of an object would be aProduct
and not aProducts
.If you are getting the opposite effect then I'm puzzled. However, check out this article - Entity Framework 4.0: Pluralization by Dan Rigsby, which perhaps explains what is happening.
您应该将类单数化,将集合复数化。例如,单数和复数是通过 ef
var customer = new Customer{}; 完成的。 //单一类
db.Customers.AddObject(客户); ObjectSEt 是复数的
customer.Orders.Add(new Order{});订单导航属性是复数的。
要了解更多信息,您还可以阅读我书中的 7-6 个食谱。
You should singularize a class and pluralize a collection. For example this is where singularazliation and pluralization is done by ef
var customer = new Customer{}; //singular class
db.Customers.AddObject(customer); ObjectSEt is pluralized
customer.Orders.Add(new Order{}); Orders navigation property is pluralized.
To learn more u can also read 7-6 recipe in my book.