Adding TimeStamps with EF 4.3 Migrations
I am using First First Migrations and changing the model to add timestamp fields to my tables. I am trying to add timetamp fields in a second migration. Here is an example 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 run the Update-Database command, I get an error that says "By default, timestamp columns of data type cannot be created. Table Users, column TimeStamp. Failed to create constraint . I have moved all data from tables, but that didn't fix the problem.
How do I add adding a timestamp field to this migration set?
Use nullable:true
. The timestamp column will have null
a column specification in it, but it will still be populated.
We are faced with this exact problem today. Our solution was to drop the table to recreate the whole thing through migration. 2 separate migration scenarios.
Obviously, this is not ideal if you want to save data, but in our case, this is not a problem.
It would be interesting to hear about a less sparse solution.
NTN.
FYI this bug was fixed in EF 5.0 beta 2. The best workaround for previous versions is the one already mentioned here: define nullable: true. Thanks for reporting an issue!