Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage GetDate() with Entity Framework

Tags:

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?

like image 496
Rod Johnson Avatar asked Apr 29 '10 20:04

Rod Johnson


People also ask

How do I run a Getdate in SQL?

SQL Server GETDATE() FunctionThe GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.

How do I change the Getdate in SQL Server?

CREATE OR ALTER FUNCTION [dbo]. GetDate RETURNS datetime2 AS BEGIN RETURN ISNULL((SELECT CurrentMoment FROM dbo. System), SYSDATETIME()); END GO -- Yes the above returns a more accurate clock than GETDATE(). Replace ALL references to GETDATE() with dbo.


1 Answers

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

like image 110
Ken Burkhardt Avatar answered Sep 20 '22 20:09

Ken Burkhardt