开发者

How does entity framework handle dates with a value of 0001/01/01

I am using the latest version of Telerik MVC extensions, ASP.NET MVC 3 with the Razor view engine, and Entity Framework 4.1 code first.

View model:

public class EditGrantApplicationViewModel
{
   // Other properties

   public DateTime Hospitali开发者_运维百科sedFromDate { get; set; }
   public DateTime HospitalisedToDate { get; set; }
}

Controller action method:

public ActionResult Create()
{
   EditGrantApplicationViewModel viewModel = new EditGrantApplicationViewModel();

   return View(viewModel);
}

Create view:

<tr>
   <td valign="top"><label>From:</label></td>
   <td>@(Html.Telerik().DatePickerFor(x => x.HospitalisedFromDate)
         .Name("HospitalisedFromDate")
      )<br>
      @Html.ValidationMessageFor(x => x.HospitalisedFromDate)
   </td>
   <td>&nbsp;</td>
   <td valign="top"><label>To:</label></td>
   <td>@(Html.Telerik().DatePickerFor(x => x.HospitalisedToDate)
         .Name("HospitalisedToDate")
      )<br>
      @Html.ValidationMessageFor(x => x.HospitalisedToDate)
   </td>
</tr>

Grant application class:

public class GrantApplication
{
   // Other properties

   public DateTime HospitalisedFromDate { get; set; }
   public DateTime HospitalisedToDate { get; set; }
}

Context:

public class HbfContext : DbContext
{
   public DbSet<Bank> Banks { get; set; }
   public DbSet<AccountType> AccountTypes { get; set; }
   public DbSet<GrantApplication> GrantApplications { get; set; }
   public DbSet<AuditEntry> AuditEntries { get; set; }
}

Grant application table:

HospitalisedFromDate datetime null
HospitalisedToDate datetime null

I have a couple of questions regarding dates and how EF 4.1 code first handling dates.

When the view is loaded the first time, HospitalisedFromDate and HospitalisedToDate is initialised to 0001/01/01. I don't have to select a date. So if nothing is selected then it will try and insert 0001/01/01 into my database table. And this is when I get an 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.

How would I fix this so that it adds a NULL value to the table column if nothing is selected?


I assume you know that the reason for this is because SqlDateTime min value and c# datetime min value are different. to have a nullable date time in c# you declare it like

public class EditGrantApplicationViewModel
{
   // Other properties

   public DateTime? HospitalisedFromDate { get; set; }
   public DateTime? HospitalisedToDate { get; set; }
}

by doing this the default value for the properties should now be null

Edit: just for more information declaring a nullable like that is equivalent to declaring it as

public class EditGrantApplicationViewModel
{
   // Other properties

   public Nullable<DateTime> HospitalisedFromDate { get; set; }
   public Nullable<DateTime> HospitalisedToDate { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜