EF6 Code First Pluralizing Tables Invalid Object Name

ef-code-first entity-framework entity-framework-6

Question

EF6 is pluralizing my Code First table names no matter what I do. I am getting the error that it can't find the pluralized table {"Invalid object name 'dbo.TestStats'."}. The table is singular in the DB as dbo.TestStat

I tried removing the convention by

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

I have also mapped it to the singular table name

    public TestStatMap()
    {
        // Primary Key
        this.HasKey(t => t.TestStatId);

        this.ToTable("TestStat");

I have even tried adding this to the OnModelCreating override

modelBuilder.Entity<TestStat>().ToTable("TestStat");

Here is the table that I am trying to map it to, which was generated by the EF power tools. What else can I try or what am I missing?

CREATE TABLE [dbo].[TestStat] (
[TestStatId] [int] NOT NULL IDENTITY,
[SeasonID] [int],
[TeamID] [int],
CONSTRAINT [PK_dbo.TestStat] PRIMARY KEY ([TestStatId])

)

Accepted Answer

Figured out my own mistake. I have two Context classes, one inheriting from the other for the repository, and I had the code below in the base context, when I moved it to the derived it now works. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why