Solved

Specifying SmallDateTime as data type for Entity Framework Code First

Posted on 2016-09-03
3
89 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

14 Experts available now in Live!

Get 1:1 Help Now