Entity Framework 4 and SYSUTCDATETIME ()

Posted by GIbboK on Stack Overflow See other posts from Stack Overflow or by GIbboK
Published on 2011-02-21T14:34:53Z Indexed on 2011/02/21 15:25 UTC
Read the original article Hit count: 379

Hi, I use EF4 and C#.

I have a Table in my DataBase (MS SQL 2008) with a default value for a column SYSUTCDATETIME ().

The Idea is to automatically add Date and Time as soon as a new record is Created.

I create my Conceptual Model using EF4, and I have created an ASP.PAGE with a DetailsView Control in INSERT MODE.

My problems:

When I create a new Record. EF is not able to insert the actual Date and Time value but it inserts instead this value 0001-01-01 00:00:00.00.

I suppose the EF is not able to use SYSUTCDATETIME () defined in my DataBase

Any idea how to solve it? Thanks

Here my SQL script

    CREATE TABLE dbo.CmsAdvertisers
(
    AdvertiserId            int             NOT NULL    IDENTITY
        CONSTRAINT PK_CmsAdvertisers_AdvertiserId PRIMARY KEY,
    DateCreated             dateTime2(2)    NOT NULL
        CONSTRAINT DF_CmsAdvertisers_DateCreated DEFAULT sysutcdatetime (),
    ReferenceAdvertiser     varchar(64)     NOT NULL,
    NoteInternal            nvarchar(256)   NOT NULL
        CONSTRAINT DF_CmsAdvertisers_NoteInternal DEFAULT ''
);

My Temporary solution: Please guys help me on this

e.Values["DateCreated"] = DateTime.UtcNow;

More info here:

http://msdn.microsoft.com/en-us/library/bb387157.aspx

How to use the default Entity Framework and default date values

http://msdn.microsoft.com/en-us/library/dd296755.aspx

© Stack Overflow or respective owner

Related posts about c#

Related posts about tsql