I store the Cart object in a database with zero date-time. This is the error I get:
Converting the datetime2 data type to the datetime data type has exceeded the value.
There are quite a few stackoverflow posts fixing fixes to this problem. However, when the code first creates the database, it creates the field as a DateTime (allows null). But for some reason, the code first tries to insert into the DateTime2 field.
I am wondering why EF creates a field in one way, but inserts a different type for the same field.
This is a domain object:
using System; using System.Collections.Generic; namespace Core.Domain.Cart { public partial class Cart : BaseEntity, ILocalizedEntity { private ICollection<Catalog> _catalogs; /// <summary> /// Gets or sets the name /// </summary> public virtual string Name { get; set; } /// <summary> /// Gets or sets the zone identifier /// </summary> public virtual int ZoneId { get; set; } /// <summary> /// Gets or sets the brand identifier /// </summary> public virtual int BrandId { get; set; } /// <summary> /// Gets or sets the customer type identifier /// </summary> public virtual int CustomerTypeId { get; set; } /// <summary> /// Gets or sets the date and time of the opening of a cart /// </summary> public virtual DateTime? OpeningDateUtc { get; set; } /// <summary> /// Gets or sets the date and time of the closing of a cart /// </summary> public virtual DateTime? ClosingDateUtc { get; set; } /// <summary> /// Gets or sets a value indicating whether the entity is online or not /// </summary> public virtual bool IsOnline { get; set; } /* Truncated for relevance */ } }
Model:
using FluentValidation.Attributes; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; using Telerik.Web.Mvc; namespace Admin.Models.Cart { [Validator(typeof(CartValidator))] public partial class CartModel : BaseNopEntityModel, ILocalizedModel<CartLocalizedModel> { public CartModel() { Locales = new List<CartLocalizedModel>(); Catalogs = new List<CatalogModel>(); UnassociatedCatalogs = new List<CatalogModel>(); } [NopResourceDisplayName("Admin.Carts.Fields.Name")] [AllowHtml] public string Name { get; set; }
So both OpeningDateUtc and ClosingDateUtc are of type DateTime ?.
Here's how to first create a database using EF code: 
OpeningDateUtc and ClosingDateUtc are created as a null DateTime field.
So why, when I save using IDBContext.SaveChanges() , the SQL generated for the query:
exec sp_executesql N'update [dbo].[Cart] set [Name] = @0, [ZoneId] = @1, [BrandId] = @2, [CustomerTypeId] = @3, [OpeningDateUtc] = @4, [ClosingDateUtc] = @5, [IsOnline] = @6, [IsReadonly] = @7, [IsPreviewMode] = @8, [CreatedOnUtc] = @9 where ([Id] = @10) ',N'@0 nvarchar(100),@1 int,@2 int,@3 int,@4 datetime2(7),@5 datetime2(7),@6 bit,@7 bit,@8 bit,@9 datetime2(7),@10 int',@0=N'Cart1',@1=7,@2=4,@3=5,@4='2013-01-09 00:00:00',@5='2013-01-18 00:00:00',@6=0,@7=0,@8=1,@9='0001-01-01 00:00:00',@10=1
The interesting part is @4 datetime2(7),@5 datetime2(7) .
I understand that I can fix this problem by adding .HasColumnType("datetime2") to the cart map, but does not answer why EF5 (and probably older versions) set them to NULL for datetime.