views:

54

answers:

3

Hi,

Can I use the SQL-function getutcdate() from entity framework to set a date field in the database when saving an entity object?

regards Freddy

A: 

You can't map directly to SQL built-in functions, but you can make EF calls to user-defined functions. Write a function that just returns GETUTCDATE(), then map to your user-defined function. I've done this before with SOUNDEX() (needed to select by soundex), and it worked out very nicely.

Jarrett Meyer
You *can* map to functions in EF 4, but in this case you don't need do, because this one is already mapped, even in EF 1.
Craig Stuntz
A: 

Why wouldn't you set the property on the entity to the UTC date before saving it?

Alternatively you could use stored procedures for insert/update/delete of your entities. The stored procedure could perform this.

confusedGeek
It isn't the same thing. Setting the property on the entity uses the app server's clock. Using `DateTime.UtcNow` uses the DB server's clock. The latter suggestion would work but is unnecessarily complicated for the problem at hand.
Craig Stuntz
+1 to you, Didn't know you could do that, Thanks.
confusedGeek
+3  A: 

Yes, you can do this. The CLR function DateTime.UtcNow is mapped to the canonical function CurrentUtcDateTime, which should translate, for SQL Server to GETUTCDATE() or similar.

Craig Stuntz