实体框架代码优先不生成迁移

时间:2016-10-19 21:27:50

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

我的应用程序面向现有数据库。在我添加了这个课程passwordhistory之后。我创建了一个迁移,但我有几个空方法:

public partial class passwordhistory : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

当我尝试运行update-database时,我收到一条错误消息,指出 db.AspNetRoles 表已经存在。我在某地读过,有人说我应该跑:

Add-Migration InitialCreate –IgnoreChanges

现在我正在尝试创建另一个迁移,以考虑我刚创建的新课程,我再次获得那些空的向上向下方法。我甚至没有在数据库中看到 PasswordHistory 表。

如何强制EF生成迁移?

这是我的模特

public class PasswordHistory
{
    public int id { get; set; }
    public string UserId { get; set; }
    public string Password { get; set; }
    public DateTime ChangeDate { get; set; }
}

public class PasswordHistoryMap : EntityTypeConfiguration<PasswordHistory>
{
    public PasswordHistoryMap()
    {
        ToTable("PasswordHistory");

        HasKey(h => h.id);

        Property(h => h.UserId)
            .HasColumnType("nvarchar(max)");

        Property(h => h.Password)
            .HasColumnType("nvarchar")
            .HasMaxLength(128);

    }
}

这是我的dbcontext类

public class myDbContext : IdentityDbContext<User>
{
    public myDbContext()
        : base("myConnection", throwIfV1Schema: false)
    {
    }

    public DbSet<PasswordHistory> PasswordsHistory { get; set; }

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new PasswordHistoryMap());

        base.OnModelCreating(modelBuilder);
    }

    public static myDbContext Create()
    {
        return new myDbContext();
    }
}

感谢您的帮助。

0 个答案:

没有答案