문제

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.

다른 팁

We just came across this exact problem today. Our solution was to delete the table then to recreate it all via migrations. 2 separate migration scripts.

Obviously not ideal if you want to retain the data but, in our case, this was not an issue.

Would be interesting to hear about a less kludgy solution.

HTH.

FYI this bug was fixed in EF 5.0 beta 2. The best workaround for previous versions is what was already mentioned here: specify nullable:true. Thanks for reporting the issue!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top