实体框架ASP MVC 4,引用错误的表

0 asp.net-mvc entity-framework code-first

我在ASP MVC 4中使用代码第一种方法与本地数据库(localdb\11.0v).我创建了我的实体

public class Service
    {
        public int ServiceID { get; set; }
        public string ServiceName { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我的DbContext

public class EFDbContext : DbContext
    {
        public DbSet<Service> Service { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我的存储库

public interface IServiceRepository
    {
        IQueryable<Service> Service { get; }
    }
Run Code Online (Sandbox Code Playgroud)

接口

public class EFServiceRepository : IServiceRepository
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<Service> Service
        {
            get { return context.Service; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我正在使用Ninject进行绑定

 ninjectKernel.Bind<IServiceRepository>().To<EFServiceRepository>();
Run Code Online (Sandbox Code Playgroud)

使用控制器引用ninject内核绑定

public ActionResult List()
        {
            return View(repo.Service);
        }
Run Code Online (Sandbox Code Playgroud)

所以这一切都很好,但它不能按照我想要的方式工作.它从"服务"表中提取一个列表,但我希望它从"服务"表中提取数据.我不明白为什么会这样.我确实有var名称复数,所以他们是"服务",但这不应该有所作为(应该吗?)我把它们全部改为"服务",希望能解决它.我真的觉得我直到现在才明白这一切.

我们有db的"业务"规则,它们需要是单数名称+我真的想要理解为什么它使用"服务"表而不是"服务"表.我只添加了"服务"表进行测试.

Bap*_*tta 6

首先,您需要将此代码添加到EFDbContext类的"使用"块中:

using System.Data.Entity.ModelConfiguration.Conventions
Run Code Online (Sandbox Code Playgroud)

然后将此代码添加到EFDbContext类中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{    
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
Run Code Online (Sandbox Code Playgroud)