Léonard Laiter Léonard Laiter - 2 months ago 18
C# Question

Entity Framework renaming mistake

I am using Entity Framework Code First in my project, and I did something quite silly I can't fix. I have renamed a table in SQL Server Management Studio, and then deleted it there. I then recreated the correct table (just an 's' that wasn't supposed to be here). Now it's not here anymore and I keep getting exceptions in my queries since EF looks for a table that does not exist anymore (even though I renamed it everywhere!). Now my table is called

RelImpUser
and it used to be
RelImpUsers
.

So I have tried recreating the table in SQL Server, then making a migration with :

public override void Down()
{
DropTable("dbo.RelImpUsers");
}


But this does not delete my table. And everytime I execute a query, it looks in
RelImpUsers
, it does not go for
RelImpUser
which is the only name I put in my code. Any ideas how to fix this? I can add some code if you want, I just felt it doesn't help much here.

Edit 1: It might have to do with the pluralization of my tables. So all my tables all had plural names, but the new one doesn't, BUT EF still pluralizes because I checked the option when creating DB Code First.

Thanks!

Answer Source

Code First uses its models to build the queries. You will need to annotate your RelImpUser class (or add comparable fluent code):

using System.ComponentModel.DataAnnotations.Schema;

[Table("RelImpUser")]
public class RelImpUser
{
    public int RelImpID { get; set; }
    public string Field1{ get; set; }
    ...
}

Fluent equivalent:

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

If you don't want pluralized names, you can turn that convention off:

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