.net - 使用 EF 4.3 迁移添加时间戳

标签 .net entity-framework entity-framework-4.3 entity-framework-migrations

我正在使用 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/

相关文章:

c# - 将自连接映射到代码优先 Entity Framework 4.3 中的集合

c# - 类如何从自身的参数化版本继承?

entity-framework - 使用 MVC2 更新带有外键的 Entity Framework v4 对象失败

silverlight - EF5 Code First 和 RIA 服务 Silverlight "Object reference not set to an instance of an object"错误构建客户端

entity-framework - 谁能发现为什么我在测试 EF 5 beta 时不断收到此错误

entity-framework - 指定的表不存在。 [ __MigrationHistory ]

c# - .NET C# - WinForm 边框

c# - 查找datagridview复选框值时如何停止空引用异常?

.net - 双入.net

c# - 每个层次结构继承的 Entity Framework 表