Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用EF 4.3迁移添加时间戳_.net_Entity Framework_Entity Framework 4.3_Entity Framework Migrations - Fatal编程技术网

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

.net 使用EF 4.3迁移添加时间戳,.net,entity-framework,entity-framework-4.3,entity-framework-migrations,.net,Entity Framework,Entity Framework 4.3,Entity Framework Migrations,我使用代码优先迁移,并修改模型,将时间戳字段添加到表中。我正在尝试在第二次迁移中添加timetamp字段。下面是我的代码的示例 public class User { public int UserId { get; set; } public string UserName { get; set; } public byte[] TimeStamp { get; set; } } public class UserModelConfiguration:

我使用代码优先迁移,并修改模型,将时间戳字段添加到表中。我正在尝试在第二次迁移中添加timetamp字段。下面是我的代码的示例

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();            
        }
    }
当我执行Update Database命令时,我收到一条错误消息,该消息说,“无法在数据类型为timestamp的列上创建默认值。表'Users',列'timestamp'。 无法创建约束“我移动了表中的所有数据,但这并没有解决问题


如何向迁移集中添加时间戳字段?

我们今天正好遇到了这个问题。我们的解决方案是删除表,然后通过迁移重新创建它。2个独立的迁移脚本

如果您想保留数据,显然不太理想,但在我们的案例中,这不是一个问题

听到一个不那么麻烦的解决方案会很有趣


HTH.

使用
nullable:true
。时间戳列将在列规范中包含
null
,但无论如何都将填充该列。

FYI此错误已在EF 5.0 beta 2中修复。以前版本的最佳解决方法是这里已经提到的:指定nullable:true。谢谢你报道这个问题

尝试nullable:true。不管怎样,时间戳都会被填满。这很有效。应用-Script指令时生成正确的SQL。添加您的答案,这样我就可以将这个solvedw标记为使用Timestamp/Rowversion数据类型或byte()生成的脚本?生成为Timestamp而不是null我也尝试过了。我确实觉得自己是个肮脏的解决方案。我向EF UserVoice提交了反馈。我只是在等待它被批准,奇怪的是。。。我生成的模式将时间戳字段显示为NOTNULL。我不知道这种行为是否取决于使用的SQL Server版本。我在SQL Server 2008 R2(Microsoft SQL Server 2008 R2(RTM)-10.50.1600.1(X64))上成功运行了代码。您在Cecil上运行的是什么版本?SQL Server 2008 x64-10.0.4064.0
public override void Up()
        {                
            AddColumn("Users", "TimeStamp", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));
        }