Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Specifying SmallDateTime as data type for Entity Framework Code First

Posted on 2016-09-03
3
Medium Priority
?
599 Views
Last Modified: 2016-09-04
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.
0
Comment
Question by:canuckconsulting
3 Comments
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 41783557
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
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 2000 total points
ID: 41783601
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
 

Author Closing Comment

by:canuckconsulting
ID: 41783604
Giannis - Perfect!  Thanks.

gustav - I'm as stingy with bytes as I am money.  :)
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question