0

I'm trying to generate automatically a default value for metadata columns in my code first database. It seems like code first migration fluent api does not handle HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)

Every table in my DB has a "CREATION_DATE" column using default value "getdate()". In my EntityTypeConfiguration i try to use:

Property(p => p.CREATION_DATE).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

This is generated for the migration:

CREATION_DATE = c.DateTime(nullable: false)

I can manually add defaultValueSql: "getdate()", but I don't want to do this every migration. I did write a SqlServerMigrationSqlGenerator for this, but it just does not seem to generate the default value.

    internal sealed class Configuration : DbMigrationsConfiguration<TheModel>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        SetSqlGenerator(SqlProviderServices.ProviderInvariantName, new CustomMigrationSqlGenerator());
    }

and in the CustomMigrationSqlGenerator class

        protected override void Generate(CreateTableOperation createTableOperation)
    {
        SetDefaultDate(createTableOperation.Columns);
        base.Generate(createTableOperation);
    }

The default value is not generated with or without the CustomMigrationSqlGenerator.

Am I missing something? I read on forums that it was an issue at some point, but the posts date back from 2010-2012.

*Edit

Content of SetDefaultDate

    private static void SetDefaultDate(IEnumerable<ColumnModel> columns)
    {
        foreach (var item in columns)
        {
            SetDefaultDate(item);
        }
    }
    private static void SetDefaultDate(ColumnModel column)
    {
        if (column.Type.Equals(System.Data.Entity.Core.Metadata.Edm.PrimitiveTypeKind.DateTime))
        {
            column.DefaultValueSql = "getdate()";
        }
    }
BenoitM
  • 97
  • 1
  • 10
  • Yes, that is the general approach: http://lancelarsen.com/entity-framework-code-first-computed-getdate/ What we do is mark our entities that have a create and modify date with an interface we can override in SaveChanges() to set the dates in EF. http://benjii.me/2014/03/track-created-and-modified-fields-automatically-with-entity-framework-code-first/ – Steve Greene Dec 02 '15 at 15:18
  • I know I could use EF to populate those fields, but my question is about generating the correct sql using code first migrations and I cannot get it to generate CREATION_DATE = c.DateTime(nullable: false, defaultValueSql: "getdate()"); I tried with dataAnnotations, even though I won't mix annotations and fluent api, but it still only generate CREATION_DATE = c.DateTime(nullable: false); – BenoitM Dec 02 '15 at 15:34
  • What does your SetDefaultDate() look like? – Steve Greene Dec 02 '15 at 15:52
  • Content of SetDefaultDate() added to original post. – BenoitM Dec 02 '15 at 15:59
  • What if you change the order of the lines inside the `Generate()` method? – JotaBe Dec 02 '15 at 16:52
  • Does not change anything. I also tried to use "PropertyModel" instead of "ColumnModel" parameter types. I also tried to compare by column.name instead of column.type, still nothing – BenoitM Dec 02 '15 at 18:07

0 Answers0