Specifying SmallDateTime as data type for Entity Framework Code First

Is there a way to force a DateTime model property to be created in SQL Server as a SmallDateTime?  This is in a asp.net MVC 5 project with EF 6.  The target db is SQL Server 2012.
canuckconsultingAsked:
Who is Participating?
 
Ioannis ParaskevopoulosConnect With a Mentor Commented:
Hi,

you can specify the data type in the On Model creating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<YourEntityName>().Property(p => p.YourPropertyName)
        .HasColumnType("smalldatetime");
}

Open in new window


or you may try the annotation:

[Column(TypeName="smalldatetime")] 

Open in new window


You may have a look here.

Giannis
0
 
Gustav BrockCIOCommented:
Not that I know of.
And why would you? Except for saving a few bytes of storage, it has no advantage of other date types.

/gustav
0
 
canuckconsultingAuthor Commented:
Giannis - Perfect!  Thanks.

gustav - I'm as stingy with bytes as I am money.  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.