EF Code First Migration从数据库中删除字段

时间:2013-12-05 14:29:37

标签: c# entity-framework entity-framework-6

我使用EF 6和Code First来创建我的数据库

  public partial class PcContext : DbContext, IPcContext
    {
        public DbSet<User> User { get; set; }
        public DbSet<Order> Order { get; set; }
        public DbSet<Product> Product { get; set; }
        public DbSet<Statistic> Statistic { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer(new EntitiesContextInitializer());                        
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<User>().
                HasMany(m => m.Statistics).
                WithOptional(o => o.User).
                WillCascadeOnDelete();

            base.OnModelCreating(modelBuilder);
        }

    }

    public class EntitiesContextInitializer : CreateDatabaseIfNotExists<PcContext>
    {

        protected override void Seed(PcContext context)
        {

            context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX IX_Email ON \"User\" (Email)");
            //skip code 

            base.Seed(context);
        }
    }

我修改了一个模型并从中删除了属性,但是表格列仍然留在数据库中,我已经开始获得异常“The model backing the 'PcContext' context has changed since the database was created.”。

所以我使用控制台和命令Enable-Migrations创建了迁移类,我不再获得异常,但数据库中的列仍然存在,并且未被迁移删除。为什么?如何从表中删除不再映射模型属性的旧列?

internal sealed class Configuration : DbMigrationsConfiguration<Context.PcContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(PC.Data.Context.PcContext context)
    {

    }
}  

0 个答案:

没有答案