为什么Add-Migration在一个表中创建重复的列?

时间:2018-03-13 13:47:03

标签: c# entity-framework ef-code-first migration ef-migrations

命令add-migration创建(在其他表中)表dbo.User。但问题是因为迁移重复了一列 - 用户名。它会生成列UsernameUsername1

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);

我的地图文件中没有重复:

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");
    }
}

我的模型文件如下所示:

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; }
}

当我自己删除这个Username1列时,我想在数据库中保存一些随机用户时出错:

“无效的列名'Username1'”

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

所以...我有:

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

而不是:

def