I have a controller:
[HttpPost]
public ActionResult Create(Auction auction)
{
var db = new EbuyDataContext();
db.Auctions.Add(auction);
db.SaveCh
I also struck this problem.
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value is a very un-useful error that one receives.
To resolve the problem I had to do the following (Using the above example):
One can find the Package Manager Console from -> Tools -> Library Package Manager -> Package Manager Console
I enabled Database Migrations in Package Manager Console -> Enable-Migrations
Then added the below code in the Controller Class:
public class Auction
{
public long Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public decimal StartPrice { get; set; }
public decimal CurrentPrice { get; set; }
// My Edit to the code Start -->
public Nullable<DateTime> StartTime { get; set; }
public Nullable<DateTime> EndTime { get; set; }}
// My Edit to the code Finish <--
}
Then in the Models Class:
[HttpPost]
// My Edit to the code Start -->
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:d}")]]
// My Edit to the code Finish <--
public ActionResult Create(Auction auction)
{
// My Edit to the code Start -->
if(ModelState.IsValid)
{
// Only Apply the DateTime if the Action is Valid...
auction.StartTime = DateTime.Now;
}
// My Edit to the code Finish <--
var db = new EbuyDataContext();
db.Auctions.Add(auction);
db.SaveChanges();
return View(auction);
}
Then Add to the Database the changes in the Package Manager Console -> Add-Migration (Your Change goes here)
Then run a Database Update in the Package Manager Console -> Update-Database
Its important to see that the Data that the Database see's is correct in the above implementation:
E.G: 17/06/2013 12:00:00 AM
This error is an amazingly stupid error but it seems to have caught a lot of people out including my self. Now I know why this error occurs it seems that it is easy to fix and get around but why such a common issue would not be fixed in the last release of MVC is beyond my comprehension.
I got the similar error when i was trying to add change_date to my contact entity in the service layer. The issue was resolved by adding the parameter from controller. Please try to add the parameter from Controller.
I had a default getdate() value on DateCreated in the DB. I wasn't setting the value in EF because I didn't think I needed to. Once I passed in the value through EF on the insert then the error went away. Why is that error there in the first place? I never figured that out.
This post - Solution to: “The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value” with Entity Framework when calling SaveChanges - proposes to use the attribute. It solved the issue for me.
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public Nullable<System.DateTime> Created { get; set; }
I recommend making the DateTime property nullable and also using attribute validation to make sure the value is within the acceptable range.
I created a custom attribute inherited from RangeAttribute to validate the date value.
public class DbDateRangeAttribute
: RangeAttribute
{
public DbDateRangeAttribute()
: base(typeof(DateTime), SqlDateTime.MinValue.Value.ToShortDateString(), SqlDateTime.MaxValue.Value.ToShortDateString())
{ }
}
And then you use the attribute like that
[DbDateRange]
public DateTime? StartTime { get; set; }
Like that, if the user enters a value outside the range of SqlDateTime (probably because of a client-side date picker issue), he will get a meaningful error message like the following:
The field StartTime must be between 1/1/1753 12:00:00 AM and 12/31/9999 12:00:00 AM.
Does your columns with datetime2 datatype accepts null values. If not then you might get this error when you are not assigning a value to these columns.
By default the CodeFirst will make non nullable columns if your model does not use nullable properties. Try converting these properties to nullable datetime.