c# - 启用的 Entity Framework 自动迁移不起作用

标签 c# entity-framework entity-framework-migrations automatic-migration

我有这个数据库配置

public class AppDbContext : DbContext 
    {

         public AppDbContext(string connectionStringOrName)
            : base(connectionStringOrName)
        {
            Database.SetInitializer(new AppDbInitializer());
        }

         public AppDbContext()
             : this("name=AppDbContext")
        {

        }

         public DbSet<User> Users { get; set; }
         public DbSet<Log> Logs { get; set; }
    }

我有这个迁移配置

public class AppDbInitializer : MigrateDatabaseToLatestVersion<AppDbContext,AppDbMigrationConfiguration>
{
}


  public class AppDbMigrationConfiguration : DbMigrationsConfiguration<AppDbContext>
    { 
        public AppDbMigrationConfiguration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(AppDbContext context)
        {
            if (context.Users.Any()) return;

            AddAdmin(context, "Admin", "admin@test.com");
        }
   }

然后我向 Log 实体添加了另一个字段。

Entity Framework 能否自动检测和应用更改?

最佳答案

如果启用了自动迁移,它应该会自动检测模型中的任何小变化。

但是对于较大的更改,例如添加新实体,我已经看到手动应用迁移,您可以使用“Add-Migration”然后运行“Update-Database”来完成

关于c# - 启用的 Entity Framework 自动迁移不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27797864/

相关文章:

c# - ManualResetEvent.Set() 什么时候可以返回 false?

c# - 将 int(数字)转换为带前导零的字符串? (4 位数)

c# - Telerik 开放访问审计追踪

.net - 从 Entity Framework 迁移到 NHibernate

c# - SaveChanges() 正在保存 Unchanged 记录

c# - 协作环境中 Entity Framework 中的迁移

c# - Mailkit:什么是 UniqueID

c# - 你如何获取字符串中的第一个字母?

entity-framework - 为什么我无法将 Entity Framework 迁移到初始状态?

entity-framework-4 - 可以使用 Entity Framework 迁移将 DateTime 字段默认为 GETDATE() 吗?