我首先使用EF代码并自动迁移.我想在我的模型中添加一个新列 - 一个布尔列,用于显示"active"(true)或"inactive"(false).如何添加此列并为数据库中已有的行设置默认值("true") - 具有自动迁移功能?
c# entity-framework ef-code-first ef-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; }
}
Run Code Online (Sandbox Code Playgroud)
我有这个迁移配置
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我向 Log 实体添加了另一个字段。
实体框架可以自动检测并应用更改吗?
c# entity-framework automatic-migration entity-framework-migrations
c# ×2