Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding TimeStamps with EF 4.3 Migrations

I'm using Code First Migrations and I'm altering my model to add timestamp fields to my tables. I'm trying to add the timetamp fields in my second migration. Here is a sample of what my code looks like

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();            
        }
    }

The generated migration looks like this

public override void Up()
        {                
            AddColumn("Users", "TimeStamp", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));
        }

When I execute the Update-Database command, I get an error message that says "Defaults cannot be created on columns of data type timestamp. Table 'Users', column 'TimeStamp'. Could not create constraint" I moved all the data from the table, but that didn't solve the issue.

How can I go about adding a timestamp field to this migration set?

like image 418
cecilphillip Avatar asked Dec 27 '25 16:12

cecilphillip


1 Answers

Use nullable:true. The timestamp column will have null in the column specification but it will be filled anyway.

like image 97
Gert Arnold Avatar answered Dec 31 '25 13:12

Gert Arnold