Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Specifying SmallDateTime as data type for Entity Framework Code First

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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