Entity Framework - update the field value to the current database server time

I pull the entity object out of the database and I need to update the date to the date / time of the database server - usually you can do this by setting it using the SQL function getDate().

How to do this in the following scenario:

var client = context.client.Where(c=>c.clientID == 1).FirstOrDefault();

// the value needs to be the value of the server current date, i.e, getDate()... not DateTime.Now
client.someDate = <somevalue>;
context.SaveChanges();
+5
source share
4 answers

Try entering the following instead of the fourth line in your example:

var dateQuery = context.CreateQuery<DateTime>("CurrentDateTime() ");
client.SomeDate = dateQuery.AsEnumerable().First();
+5
source

DateTime.Now will provide the current date from your server (web server, not the database server). if client.someDate is a DateTime field, you can assign DateTime.Now and update it to the database.

0
source

VB.NET EF6/MVC5.

: getdate(), getdate() . , . , getdate() , .

: getdate() getdate() ( , OUTPUT INSERTED.theTime). SQL-.

Dim theTime As DateTime? = db.Database.SqlQuery(Of DateTime?) _
("UPDATE myTable SET theTime = getdate() OUTPUT INSERTED.theTime WHERE someRecordID = @someRecordID", New SqlParameter("@someRecordID", someRecordID)).First()

, - ( MS, getdate() ?). , - .

, :

Dim craftBeer = context.CraftBeers.Find(id)
... (update other craftBeer properties) ...
craftBeer.date_emptied = SqlServer.SqlFunction.GetDate
context.Entry(craftBeer).State = EntityState.Modfied
context.SaveChanges()

... , , SqlServer.SqlFunctions.GetDate, , EF, .

0

, , . , SQL Server, RETURNS GETDATE(). SQL . , , .

0

All Articles