Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF migrations and timestamp column, cannot update/run

I have an object in EF6 that i forgot to inherit from my auditableEntity class. This class has a configuration like so

public abstract class AuditableEntityConfig<TEntity> : BaseEntityConfig<TEntity> where TEntity : AuditableEntity
{
    public AuditableEntityConfig()
        : base()
    {

        this.Property(e => e.RowVersion)
            .IsRowVersion();

    }
}

Now i have updated my entity to inherit from this class and now upon running my code, i always get an error saying

Cannot alter column 'RowVersion' to be data type timestamp.

Is there anyway i can stop EF trying to set this column to be timestamp, and maybe i drop and recreate the table myself instead?

like image 209
Gillardo Avatar asked Feb 26 '15 06:02

Gillardo


2 Answers

Replace one line AlterColumn with 2 lines DropColumn and AddColumn in Up() method.

        public override void Up()
        {
            DropColumn("dbo.Cities", "RowVersion", null);
            AddColumn("dbo.Cities", "RowVersion", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));
        }

        public override void Down()
        {
            AlterColumn("dbo.Cities", "RowVersion", c => c.Binary());
        }
like image 150
Mark Macneil Bikeio Avatar answered Oct 04 '22 22:10

Mark Macneil Bikeio


Step 1. Edit table definition mannualy:

   [RowVersion]   TIMESTAMP NOT NULL

Step 2. Go to NameOfMigrationAdded.cs in Migrations directory and comment this line in Up() method:

   AlterColumn("dbo.YOUR_TABLE_NAME_HERE", "RowVersion", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));

Step 3. Run 'Update-Database' command in PM console

Now it works ;)

Step 4. Return to previous migration and edit Up() method by adding:

 Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),

in CreateTable( ... ) or AddColumn( ... )

Ex:

  AddColumn("dbo.Rows", "RowVersion", c => c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"));
like image 36
Łukasz Jankowski Avatar answered Oct 04 '22 21:10

Łukasz Jankowski