Also, when DateTime.Now is used when writing to the database

It is well known that in the database you must store the date in universal time coordinate.

I am looking for a way to find out when a developer abuses DateTime.Now when writing to a database.

We use Sql Server 2008 with EF4 or nHibernate 3.0.

Is it possible to intercept a datetime value when it contains time zone information at a low level, for example, either on an Sql server or in NH / EF?

+3
source share
6 answers

You can add the following event listener:

public class DateTimeEventListener : IPreUpdateEventListener,
                                     IPreInsertEventListener
{
    public bool OnPreUpdate(PreUpdateEvent e)
    {
        foreach (var value in e.State)
            if (value is DateTime && ((DateTime)value).Kind != DateTimeKind.Utc)
                throw new Exception("Non-UTC DateTime used");
    }

    public bool OnPreInsert(PreInsertEvent e)
    { /*Same as OnPreUpdate*/ }
}

( , . )

+3

, ( - ), DateTime, . UTC.

EF NHibernate UTC Now, .

, , DateTime.Now .

0

. , , - .

, DateTime.Now SQL- . DateTime.Now. . , UTC, , - . .

0

, UTC , , pre-commit.

, . , IMO.

0

. . :

a) ,

b) . DateTimeOffset .

0
source

All Articles