2

I know there are a ton of posts about this issue but none of them seem to solve my problem.

Here's the scenario:

I have a CreateDate DateTime column in my MS SQL Server database User table that is non-nullable and is automatically set using GetDate() method in "Default Value or Binding" setting.

I am able to create a User just fine with the standard EF Insert but when I try to update the user, I get this error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

What is the trick to not having the EF worry about the CreateDate column for updates? I have the StoreGenerationPattern = Identity but that isn't helping.

Here are the EF properties for my Entity Property: http://screencast.com/t/8ndQRn9N

And here is my Update method: http://screencast.com/t/UXIzhkhR

RichC
  • 7,829
  • 21
  • 85
  • 149

2 Answers2

4

Have you tried setting the StoreGeneratedPattern attribute to 'Computed'. Failing that, as a last resort, try changing the column's datatype to datetime2.

[The problem arises because the .NET DateTime.MinValue equals 0001-1-1 but Sql Server DateTime covers 1753-1-1 through 9999-12-31]

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    I realize that datetime2 covers a larger span of time but why would I need more time if I'm just setting the create date (which would be the current date)? The issue is that it keeps trying to update the createdate to 0001-1-1 instead of not updating it at all since it's a createdate and gets created in the database upon creation of the record and should never get updated. It should just leave it alone and not worry about it like the Entity Key. – RichC Mar 24 '11 at 23:57
2

I know this is an old question but maybe this may help someone:

The problem may be a bug that causes StoreGeneratedPattern in the Conceptual section (CSDL) of the .edmx file to be out of sync with the StoreGeneratedPattern in the Storage section (SSDL). The GetDate() default is never called because - unless you assign the date from your client-side code - the default value of DateTime.MinValue is used.

The good news is, it's fixed. Look for KB2561001: http://archive.msdn.microsoft.com/KB2561001. I don't know if this is fixed in the subsequent EF 4.3 release or you still have to apply the hotfix.

HiredMind
  • 1,827
  • 17
  • 27