1

Is there a way to obtain the default value of a column in Entity Framework? I can't seem to create an Entity Data Model of the INFORMATION_SCHEMA.COLUMNS table like I would a regular table.

Ordinarily, with a MySQLConnection, I would do something like

SELECT COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = 'tablename' AND COLUMN_NAME = 'columnname';

Nils Guillermin
  • 1,867
  • 3
  • 21
  • 51

1 Answers1

0

Possible duplicate of Get default sql value from Entity Framework class, so I'll quote:

In case you have access to the generated DbContext, you can use the metadata provided by the DbContext.Model instead of reflection (But you can use reflection if you set your DefaultValues using Attributes, why not! Just use MemberInfo.CustomAttributes)

For instance:

foreach (var type in db.Model.GetEntityTypes())
{
    foreach (var property in type.GetProperties())
    {
        var defaultValue = property.Relational().DefaultValue;
    }
}
Daniel Vygolov
  • 884
  • 2
  • 13
  • 26
  • This doesn't seem to be available for Code First (which makes sense). It turns out I can do a `.Database.SqlQuery` though, I think that may serve my purposes. – Nils Guillermin Sep 18 '18 at 18:32