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.
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 {
// ...
}
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();
The generated sql looks like:
Select [Extent1].[Username] From [Users] as [Extent1]
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
I wonder why Microsoft implement EF 5.x that way rather than same to EF 3.0?
Updated:
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);
}
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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…