views:

703

answers:

2

I have a column like this in 1 of my database tables

DateCreated, datetime, default(GetDate()), not null

I am trying to use the Entity Framework to do an insert on this table like this...

        PlaygroundEntities context = new PlaygroundEntities();

        Person p = new Person
        {
            Status = PersonStatus.Alive,
            BirthDate = new DateTime(1982,3,18),
            Name = "Joe Smith"
        };

        context.AddToPeople(p);
        context.SaveChanges();

When i run this code i get the following 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.

So i tried setting the StoreGeneratedPattern to computed... same thing, then identity... same thing. Any ideas?

A: 

Changing type of DateCreated to datetime2 might solve the problem.

datetime 2007-05-08 12:35:29.123

datetime2 2007-05-08 12:35:29. 12345

Ref: http://technet.microsoft.com/en-us/library/bb677335.aspx67

rk1990
that solves the exception, but the default (GetDate()) is not getting called on the insert.
wcpro
actually, it appears it does work, but the .net code is setting like the datetime.minvalue by default. Is there a way to prevent it from doing this?
wcpro
got it working by using Calculated storegeneratedpattern
wcpro
I have this exact problem, can you write up a proper answer please wcpro?
Aidan
A: 

You have to manually edit the edmx xml and set your SSDL StoreGeneratedPattern attributes to identity or computed. But whenever you update your edmx via the designer your changes will get overwritten.

This is a known issue. Please see the following links for more details:

Microsoft Connect Ticket

Using a GUID as an EntityKey in Entity Framework 4

KenB

related questions