How to create a default constraint in EF code-first and Azure?

Viewed 1175

I have the following code-first Entity Framework code that I'm using with Azure Mobile services:

public class BookContext : DbContext
{
private const string connectionStringName = "Name=MS_TableConnectionString";

public BookContext() : base(connectionStringName) { } 

// The collections queried directly.
public DbSet<Book> Books{ get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Standard code to add the "service" columns such as CreatedAt, UpdatedAt, and Deleted.
    modelBuilder.Conventions.Add(
        new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
            "ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));

    // Add a default value 0 on the Deleted column. The following code does not work.
    modelBuilder.Entity<Book>()
                .Property(p => p.Deleted)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
}
}

As noted above, the code is supposed to add a default constraint on the Deleted column, but it does not do anything. I'd like to achieve an equivalent of the following SQL statement as the EF code-first:

ALTER TABLE dbo.Books ADD CONSTRAINT DF_BooksDeleted DEFAULT 0 FOR Deleted

Apparently, I must be doing something wrong. Could anyone point me in the right direction?

1 Answers
Related