为什么我的实体框架代码不是第一次多元化? [英] Why isn't my Entity Framework Code First Pluralization working?
问题描述
using System;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
public class CustomApp_User
{
[Key]
public int UserID {get;组; }
[MaxLength(50)]
public string Username {get;组; }
[MaxLength(250)]
public string Email {get;组; }
public DateTime DateCreated {get;组; }
public DateTime DateModified {get;组; }
}
在数据库中我有一个名为CustomApp_Users的表以上匹配注意它最后有s。
然后我有:
using System;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Data.Entity;
public class CustomAppDB:DbContext
{
public DbSet< CustomApp_User> CustomApp_Users {get;组;我希望EF codefirst能够多元化,以便在CustomApp_Users中找到CustomApp_Users。这个数据库是一般的工作原理。但是我得到错误:
无效的对象名称'dbo.CustomApp_User'。
看起来不是复数表名。我不知道为什么。与此数据库不同的是,主键不遵循常规约定,因此我使用[Key]注释。
我知道如果我使用[表]我的类的注释将工作:
[Table(CustomApp_Users)]
但是,我想知道为什么复数是
解决方案这是因为 PluralizationService 无法将其复用。如果传递CustomApp_User,它返回相同的字符串。不幸的是,您无法自定义此服务。所以你需要明确配置表名。
I'm using Entity Framework Code First. Usually I have no problem, but for a database I'm working with I keep getting errors that it can't find the table in the SQL Server database. Here is what my class looks like:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class CustomApp_User
{
[Key]
public int UserID { get; set; }
[MaxLength(50)]
public string Username { get; set; }
[MaxLength(250)]
public string Email { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
}
In the database I have a table called "CustomApp_Users" to match the above. Note it has the "s" at the end.
And then I have:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
public class CustomAppDB : DbContext
{
public DbSet<CustomApp_User> CustomApp_Users { get; set; }
}
I expected EF codefirst to pluralize so that it would find "CustomApp_Users" in the database since this is how it usually works. But instead I get the error:
Invalid object name 'dbo.CustomApp_User'.
It appears it's not pluralizing the table name. I can't figure out why. One thing different with this database is that the Primary Keys do not follow the normal convention so I use the [Key] annotation.
I do know that if I use the [Table] annotation for my class it will work:
[Table("CustomApp_Users")]
But, I'd like to find out why the pluralization is not working the way I thought it would.
解决方案 That is because the PluralizationService in EF can not pluralize it. It returns the same string if you pass it "CustomApp_User". Unfortunately you can not customize this service. So you need to configure the table name explicitly.
这篇关于为什么我的实体框架代码不是第一次多元化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!