Entity Framework migration error rename table

0

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?

.net-core
.net-core-3.1
asked on Stack Overflow Nov 20, 2020 by allpacka

1 Answer

1

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");
answered on Stack Overflow Nov 20, 2020 by (unknown user)

User contributions licensed under CC BY-SA 3.0