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?
Use nullable:true. The timestamp column will have null in the column specification but it will be filled anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With