26

my model includes a nullable datetime property.Im using CodeFirst

 public DateTime? GoDate { get; set; }

I would like to insert a nullvalue into that field but EF inserts the usual 00/00/0001 date instead of null, which also gives me an error.

im using microsoft sql server 2012.

DateTime? date = null;
var entity = new Model()
                {
                    GoDate = date
                };

DataContext.Models.Add(entity);
DataContext.SaveChanges();

give me the error.

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.

That is because sql server datetime cant have 00/00/0001, which EF automatically generates once it inserts a null datetime into the database.

I want to insert null into the db.

5
  • Which database are you using? SQL Server? When you say it "gives [you] an error", what error do you receive? Are you using DB first or code first? Commented Oct 26, 2014 at 0:13
  • Where is the code for insert?? Commented Oct 26, 2014 at 0:17
  • Using Nullable<DateTime> or DateTime? should make it accept NULL. Is the column in the database marked as "NOT NULL"? Commented Oct 26, 2014 at 0:30
  • in the database the field is marked as NULL.Again the problem is that entity framework generates a datetime value of 00/00/0001 if the datetime is null and the sql server doesnt have 00/00/0001 for date.I need to stop entity generating that default value. Commented Oct 26, 2014 at 0:31
  • Check the generated SQL when doing SaveChanges() and post it here. Commented Oct 26, 2014 at 0:57

4 Answers 4

18

EF inserts the usual 00/00/0001 date instead of null

There's nothing usual about it. Left to its own devices, EF 6.1 inserts NULL.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var DataContext = new StackOverflowContext();

            DateTime? date = null;
            var entity = new Model()
            {
                GoDate = date
            };

            DataContext.Models.Add(entity);
            DataContext.SaveChanges();
        }
    }

    class Model
    {
        public int ModelId { get; set; }

        public DateTime? GoDate { get; set; }
    }

    class StackOverflowContext : DbContext
    {
        public DbSet<Model> Models { get; set; }
    }
}

enter image description here

It's most likely your mappings or database schema are wrong.

Sign up to request clarification or add additional context in comments.

Comments

4

You can use something like this in your model. then sending null from your controller will work fine.

 [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Date")]
        public DateTime? Date { get; set; }

Notice the ? after DateTime.

1 Comment

I notice the "?" after the DateTime, but not before?
3

Or you can just set like this:

var entity= new Model
{
    GoDate = (DateTime?) null
}
DataContext.Models.Add(entity);
DataContext.SaveChanges();

I think it is a little bit cleaner.

Comments

1

You Can use this :

Nullable<DateTime> date=null;
var entity = new Model()
{
    GoDate = date
};

DataContext.Models.Add(entity);
DataContext.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.