Search code examples
c#entity-frameworkef-code-firstmigrationentity-framework-migrations

Why does Add-Migration create duplicate columns in one table?


Command add-migration create (among other tables) table dbo.User. But the problem is because migration duplicates one column - Username. It makes columns Username and Username1.

CreateTable(
            "dbo.User",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    FirstName = c.String(),
                    LastName = c.String(),
                    DateOfBirth = c.DateTime(),
                    Username = c.String(nullable: false),
                    Username1 = c.String(),
                    Password = c.String(nullable: false),
                    LastTimeLogedIn = c.DateTime(),
                    RoleId = c.Int(nullable: false),
                    UserGenderId = c.Int(),
                    ChoosenTeamId = c.Int(),
                    AdministratorTeamId = c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.UserRole", t => t.RoleId)
            .ForeignKey("dbo.Team", t => t.ChoosenTeamId)
            .ForeignKey("dbo.UserGender", t => t.UserGenderId)
            .Index(t => t.RoleId)
            .Index(t => t.UserGenderId)
            .Index(t => t.ChoosenTeamId);

There is no duplicate in my mapping file:

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        // Table Mappings
        ToTable("User");

        // Primary Key
        HasKey(t => t.Id);

        // Relationships
        HasRequired(t => t.Role)
         .WithMany(t => t.Users)
         .HasForeignKey(d => d.RoleId);
        HasOptional(t => t.UserGender)
         .WithMany(t => t.Users)
         .HasForeignKey(d => d.UserGenderId);
        HasOptional(t => t.Team)
         .WithMany(t => t.Users)
         .HasForeignKey(d => d.ChoosenTeamId);

        // Properties
        Property(t => t.Id)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
         .HasColumnName("Id");

        Property(t => t.FirstName)
         .HasColumnName("FirstName");

        Property(t => t.LastName)
         .HasColumnName("LastName");

        Property(t => t.DateOfBirth)
         .HasColumnName("DateOfBirth");

        Property(t => t.Email)
         .IsRequired()
         .HasColumnName("Email");

        Property(t => t.Email)
        .IsRequired()
        .HasColumnName("Username");

        Property(t => t.Password)
         .IsRequired()
         .HasColumnName("Password");

        Property(t => t.LastTimeLogedIn)
         .HasColumnName("LastTimeLogedIn");

        Property(t => t.RoleId)
         .IsRequired()
         .HasColumnName("RoleId");

        Property(t => t.UserGenderId)
         .HasColumnName("UserGenderId");

        Property(t => t.ChoosenTeamId)
         .HasColumnName("ChoosenTeamId");

        Property(t => t.AdministratorTeamId)
         .HasColumnName("AdministratorTeamId");
    }
}

and my model file look like this:

public class User : IUser
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public DateTime? LastTimeLogedIn { get; set; }
    public int RoleId { get; set; }
    public virtual UserRole Role { get; set; }
    public int? UserGenderId { get; set; }
    public virtual UserGender UserGender { get; set; }
    public int? ChoosenTeamId { get; set; }
    public int? AdministratorTeamId { get; set; }
    public virtual Team Team { get; set; }
}

When I delete this Username1 column by myself, I have error when I want to save some random user in database:

"Invalid column name 'Username1'"

Any idea why?


Solution

  • So... I have:

    Property(t => t.Email)
        .IsRequired()
        .HasColumnName("Username");
    

    instead of:

    Property(t => t.Username)
        .IsRequired()
        .HasColumnName("Username");