Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to force all DateTime properties to be modeled as DateTime2?

In Entity Framework 6 Code First, is there a way to force all DateTime properties to be modeled as DateTime2?

I know that I can do

.HasColumnType("datetime2")

on each individual DateTime property, but I'm wondering if there's a way to set it as a default for all DateTime properties.

like image 489
Eric Avatar asked Feb 14 '13 22:02

Eric


1 Answers

Yes.

In Entity Framework 6's Fluent API, you can update all DateTime properties using this one line:

modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));
like image 113
Matt DeKrey Avatar answered Oct 02 '22 23:10

Matt DeKrey