Solved

Specifying SmallDateTime as data type for Entity Framework Code First

Posted on 2016-09-03
3
60 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 49

Expert Comment

by:Gustav Brock
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Giannis - Perfect!  Thanks.

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now