I'm using a Entity data model connected to my SQLDatabase.

One specific column is datetime.

When I declare the value as Datetime.Now I get the following error:

Inner Exception: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Here is my code snippet:

ReworkEntities dbContext = new ReworkEntities();

        Carton carton = new Carton();

        Carton_Details detail1 = new Carton_Details();
        detail1.Detail = "Some Detail Here";
        detail1.DateOfSub = DateTime.Now;

        carton.Carton_Details.Add(detail1);

        dbContext.AddToCartons(carton);
        dbContext.SaveChanges();

I spent hour on Google and found allot of similar problems but none seem to work for me.

They suggested to use .toSting("M/dd/yyyy H:mmss tt"), I works for the string value but I need the DateTime value and when converting it back it is still the same.

Also tried .Parse , .ParseExact and also no success.

Another post suggests changing the SQL Database datetime format but I cant do that as it is our code-standard to use it like that.

Any suggestions on how to fix the problem would be greatly appreciated.

Thanx.

有帮助吗?

解决方案

I faced a similar problem, just put in the constructor of your entity a value for the attribute, the code follows.

public class Carton_Details
{
    public DateTime DateOfSub { get; set; }

    public Carton_Details()
    {
        this.DateOfSub = DateTime.Now;
    }
}

I hope I helped.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top