Created
September 18, 2016 12:27
-
-
Save rasoulian/be4d11034f4c3a84d36943fd1e523bf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace App.Data.Migrations | |
{ | |
internal sealed class Configuration : DbMigrationsConfiguration<DataContext> | |
{ | |
public Configuration() | |
{ | |
AutomaticMigrationsEnabled = false; | |
SetSqlGenerator("System.Data.SqlClient", new CustomSqlServerMigrationSqlGenerator()); | |
} | |
} | |
internal class CustomSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator | |
{ | |
protected override void Generate(AddColumnOperation addColumnOperation) | |
{ | |
SetCreatedUtcColumn(addColumnOperation.Column); | |
base.Generate(addColumnOperation); | |
} | |
protected override void Generate(CreateTableOperation createTableOperation) | |
{ | |
SetCreatedUtcColumn(createTableOperation.Columns); | |
base.Generate(createTableOperation); | |
} | |
private static void SetCreatedUtcColumn(IEnumerable<ColumnModel> columns) | |
{ | |
foreach (var columnModel in columns) | |
{ | |
SetCreatedUtcColumn(columnModel); | |
} | |
} | |
private static void SetCreatedUtcColumn(PropertyModel column) | |
{ | |
if (column.Name == "CreationOn" || column.Name == "UpdatedOn") | |
{ | |
column.DefaultValueSql = "GETDATE()"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment