How to use Entity Framework's GetDate() method

entity-framework sql-server-2008

Question

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?

1
20
4/29/2010 8:28:59 PM

Accepted Answer

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

2
4/29/2010 8:34:20 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow