Get server time not local one for EF entity

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Get server time not local one for EF entity

Post by bairog » Tue 20 May 2014 10:54

I use dotConnect for SQLite 5.2.146 + Entity Framework 6.1.0 and Code-First approach.

Suppose I have a following Entity:

Code: Select all

public class User
    {
        [Key]
        public int Id { get; set; }
        public String Name{ get; set; }
        public DateTime LastOperationDate{ get; set; }
    }
My database is located on server.
What I want to achieve is LastOperationDate to be server time, not my local machine time.

So what should I use instead of

Code: Select all

LastOperationDate = DateTime.Now
Maybe some SQL?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Get server time not local one for EF entity

Post by Shalex » Wed 21 May 2014 11:50

Please make the code to generate the LastOperationDate column in the database with the datetime('now') default value. You can set sql default value with Code-First Migrations.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Get server time not local one for EF entity

Post by bairog » Wed 21 May 2014 12:33

I'm not only talking about field's default value.
LastOperationDate is updated frequenty

Code: Select all

private void UpdateUser(int Id, String NewName)
LastOperationDate should be set to server time, at which that function above was called.
What should I use in that case?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Get server time not local one for EF entity

Post by Shalex » Fri 23 May 2014 13:17

SQLite is a local database. Even if you locate the file of database remotely in local network, the database engine (sqlite3.dll) will be placed on the workstation where the code is executed. So datetime('now') in SQL will be equal to DateTime.Now in .NET code.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Get server time not local one for EF entity

Post by bairog » Mon 26 May 2014 09:47

Shalex wrote:So datetime('now') in SQL will be equal to DateTime.Now in .NET code.
Thx, I've missed that moment..

Post Reply