By default EF Core code first on MS SQL server generates DateTime columns as DateTime2(7). I want to change default behaviour. When no explicit precision atribute is given than Datetime2(3) should be generated, like [Precision(3)] attribute is used.
CodePudding user response:
Override this method in your DBContext.
protected override void ConfigureConventions(
ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<datetime>()
.HasPrecision(3);
}
CodePudding user response:
I have found a working solution. I have to use OnModelCreating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?))
{
if (property.GetPrecision() == null)
{
property.SetPrecision(3);
}
}
}
}
}