为什么EF 5.x的对表使用复数的名字吗? [英] Why does EF 5.x use plural name for table?

查看:390
本文介绍了为什么EF 5.x的对表使用复数的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ORM框架一些经验,如Hibernate,甚至实体框架3.0。

I've had some experiences in ORM framework such as Hibernate, even Entity Framework 3.0.

默认情况下,这些框架使用单数名称表。例如,类用户将映射到表用户。

By default, those frameworks use singular name for table. For example, class User will map to table User.

但是当我通过使用Visual Studio 2012迁移到EF 5.x的,它使用复数名称,并导致除非许多错误我手动使用映射类 TableAttribute 为:

But when I migrate to EF 5.x by using Visual Studio 2012, it uses plural name and causes many errors unless I manually map that class by using TableAttribute as:

[Table("User")]
public class User {
    // ...
}

没有 TableAttribute ,如果我有一个的DbContext 如下:

Without TableAttribute, if I have a DbContext as below:

public CustomContext : DbContext {
    // ...
    public DbSet<User> Users { get; set; }
}



然后调用:

Then calling:

var list = db.Users.ToList();

生成的SQL是这样的:

The generated sql looks like:

Select [Extent1].[Username] From [Users] as [Extent1]

因此,将发生错误,因为我没有名为用户的任何表。 Futhermore,我指的名字表中单数形式到复数形式。你可以看到为什么在此链接

Therefore, the error will occur because I don't have any table named Users. Futhermore, I refer to name tables in singular form to plural form. You can see why in this link

我不知道为什么微软实施EF 5.x的这种方式,而不是同为3.0 EF

I wonder why Microsoft implement EF 5.x that way rather than same to EF 3.0?

更新:

我们可以告诉EF不能在上下文类使用此代码中使用复数形式的名称:

We can tell EF not use pluralized name by using this code in the context class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    base.OnModelCreating(modelBuilder);
}



即使EF工具生成多元化的名字,它应该保持映射到原来的数据库,不应该吗?由于一些用户可能想改变自己的类字段的几个名字。 EF 3.0的作品不错,但EF 5.x的没有。

Even if EF tool generates pluralized name, it should keep the mappings to the original database, shouldn't it? Because some users might want to change a few name of fields on their class. EF 3.0 works well, but EF 5.x does not.

家伙,你可以​​给我一个理由,请!

Guys, can you give my a reason, please!

推荐答案

该公约在的 PluralizingTableNameConvention 通过默认定义约定 DbModelBuilder.Conventions

如果您想从所有表中排除,您可以使用代码的这个问题

If you want to exclude it from all tables, you can use the code from this question.

为什么这样做是这样,我不知道的方式,但我必须说,我个人而言,在认为表名应该复数阵营:)

Why this is done the way it is, I do not know, but I must say that I am, personally, in the camp that thinks that table names should be pluralized :)

提供一个SQL Server实例的实例数据库,罗斯文有复数和AdventureWorks的使用单数形式,所以在最好,没有建立的标准。我已经有相当多的讨论,赞成或反对每个人,但有一点大家可以达成一致的是,一旦选择一个命名策略的时候,你应该坚持下去。

Of the example databases provided with an SQL Server instance, Northwind has plurals, and AdventureWorks uses a singular form, so at best, there is no established standard. I've had quite a few discussion for or against each one, but one thing that everybody can agree on is that when once pick a naming strategy, you should stick with it.

这篇关于为什么EF 5.x的对表使用复数的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆