我正在使用 Code First Migrations,并且正在更改我的模型以将时间戳字段添加到我的表中。我正在尝试在我的第二次迁移中添加时间戳字段。这是我的代码的示例
public class User {
public int UserId { get; set; }
public string UserName { get; set; }
public byte[] TimeStamp { get; set; }
}
public class UserModelConfiguration: EntityTypeConfiguration<User> {
public UserModelConfiguration() {
Property(p => p.UserName).IsRequired().HasMaxLength(250);
Property(p => p.TimeStamp).IsRowVersion();
}
}
生成的迁移看起来像这样
public override void Up()
{
AddColumn("Users", "TimeStamp", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));
}
当我执行 Update-Database 命令时,我收到一条错误消息,内容为 “无法在时间戳数据类型的列上创建默认值。表‘Users’,列‘TimeStamp’。 无法创建约束”我从表中移动了所有数据,但这并没有解决问题。
如何向此迁移集添加时间戳字段?
最佳答案
使用 nullable:true
。时间戳列在列规范中将为 null
,但无论如何都会被填充。
关于.net - 使用 EF 4.3 迁移添加时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9597766/