Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6783555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:52:42+00:00 2026-05-26T16:52:42+00:00

I have an example class book : public class Book { [Key] public int

  • 0

I have an example class book:

public class Book
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime DateAdded { get; set; }
}

When I attempt to add a new book to the BookDb context…

using (BookDb db = new BookDb())
{
    Book book = new Book {
        Name = "Some name",
        DateAdded = DateTime.Now
    };

    db.Books.Add(book);
    db.SaveChanges();
}

… an error is thrown:

System.Data.SqlClient.SqlException: The conversion of a datetime2 data
type to a datetime data type resulted in an out-of-range value. The
statement has been terminated.


I’ve found that the cause of this is the incompatible datetime types between .NET and SQL Server. There is a way to tell EF to use SQL Server’s format in traditional Entity Framework, but how do I do it in Code-First Entity Framework?

I am using EF4 on .NET 4 (MVC 3 web app) and SQL Server 2008 Express.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:52:42+00:00Added an answer on May 26, 2026 at 4:52 pm

    You can specify the type in Fluent API:

    modelBuilder.Entity<Book>()
        .Property(f => f.DateTimeAdded)
        .HasColumnType("datetime2");
    

    This creates a datetime2(7) column in the database. If you want to finetune the precision you can use:

    modelBuilder.Entity<Book>()
        .Property(f => f.DateTimeAdded)
        .HasColumnType("datetime2")
        .HasPrecision(0);
    

    … for a datetime2(0) column in the DB.

    However, the code you have shown in your question works because the datetime type allows to store dates back to around 1750. The exception occurs only for earlier dates. A common reason for this exception is an uninitialized DateTime property because it represents the year 0001 which can’t be stored in a datetime column in SQL Server.

    There is no corresponding attribute to define this with data annotations. It’s only possible with Fluent API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have for example a class: public class Book { public int BookId {
I have a Patient class: class Patient { public string First_Name { get; set;
I have an example class containing two data points: public enum Sort { First,
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
Let's have a following example: public class X { } public class Y {
I have a class, lets call it Book with a property called Name. With
Take the following example: class BookManager { ... }; class Book { public: void
For example I have 3 books: Booknumber (int) , Booktitle (string) , Booklanguage (string)
I have following entities and DbContext: public class Customer { [Key] public Guid Guid
I have the following C# classes : public class Books { public List<Book> BookList;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.