allpacka
allpacka

Reputation: 27

Entity Framework migration error rename table

I tried to add new model to my project with code-first migration. The model looks like this:

public class Renting
{
    [Key]
    public int Id { get; set; }
    
    public string RequesterEmail { get; set; }
    public string RequesterName { get; set; }

    public int RoomNumber{ get; set; }
    public RentalOption RentalOption { get; set; }
    public User User { get; set; }
}

the User and Rentaloption will connect these tables. I added a migration, then I tried to update-database from 'package manager console'.

then I got an error message:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (112ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      EXEC sp_rename N'[User]', N'Users';
Failed executing DbCommand (112ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
EXEC sp_rename N'[User]', N'Users';
Microsoft.Data.SqlClient.SqlException (0x80131904): Error: The new name 'Users' is already in use as a object name and would cause a duplicate that is not permitted.

I did not changed the User model. How can I solve this?

Upvotes: 0

Views: 786

Answers (1)

user8551329
user8551329

Reputation:

the entity framework automatically pluralize the table names : User will become Users

check this link ..

Entity Framework Code First naming conventions - back to plural table names?

do this:

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

you can give table names like this: (in OnModelCreating() )

modelBuilder.Entity<User>().ToTable("Users");

Upvotes: 1

Related Questions