Entity Framework Code First: which DataType attribute for DateTime2?

c# dbcontext entity-framework

Question

When utilizing Entity Framework Code First, the default conventions may fail to build the desired database type. For instance, a property of the typeSystem.DateTime creates a type of database columnDateTime How to make it have a certain type if you want it todatetime2 (the DateTime (the kind who is unaffected by time zones and daylight saving time)?

With Data Annotations, it is possible to use a to indicate the needed database type.DataTypeAtrribute . A parameter named Enumeration of DataTypes is accepted by one of the builders of DataTypeAttribute. Consequently, one could say something like:

[DataType(DataType.DateTime)]
public DateTime DateOfBirth {get; set;}

There are numerous types in the DataType enumeration type, but a value forDateTime2 .

Using Fluent API would be an alternative strategy. Develop aDateTime2 In the DBContext.OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>().Property(p => p.BirthDate)
        .HasColumnType("datetime2");
}

There is a an additional constructor that takes a string in the DataTypeAttribute. A definition of this string is

The name of the custom field template to associate with the data field.

Therefore, it stands to reason that the following would be sufficient to generate a datetime2:

[DataType("datetime2")]
public DateTime DateOfBirth {get; set;}

Alas, this doesn't work. The generated column continues to have theDateTime format.

Which string should be entered into the constructor to generate a datetime2?

1
12
4/6/2017 7:43:02 AM

Accepted Answer

For those who are still interested in how property column types are defined. You can specify that every value of a certain type should have a specific database type starting with EF version 6.0.

To do this, inDbContext.OnModelCreating DbModelBuilder.Properties is used.

You won't need to build characteristics or a fluent API for every DateTime if you do this. Consistency and using the same column type across the board are easy to achieve. Similarly, even if more decimals are added later, you can give all of them the same precision.

Consider that you wish to define everySystem.DateTime ought to be a column typeDateTime2 ; everySystem.Decimal ought to have a column type with the required level of precision. DbContext is where you would type:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // every property of type DateTime should have a column type of "datetime2":
    modelBuilder.Properties<DateTime>()
      .Configure(property => property.HasColumnType("datetime2"));
    // every property of type decimal should have a precision of 19
    // and a scale of 8:
    modelBuilder.Properties<decimal>()
        .Configure(property => property.HasPrecision(19, 8));
}
10
6/19/2018 7:02:14 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow