How to ask database server for current time and time using entity structure?

I do not want my current ModifeidDateTime to be server machine time. Every time I want to update or add a person to a database on SQL Server 2008, I want to populate the ModifiedDateTime file. It does not look like I can change the update request as in the data adapter command when I work with a dataset and define for my ModifiedDateTime registered as Get = GetDate (). I created a stored function for retutn me the value of the GetDate () method, but I have a problem importing a procedure that returns values ​​as int, string ... or it doesn’t matter at all, it’s just the value of the entity like Person, for example, in my case, why is it? Anyway, it would be very helpful if you could provide me with any solution that works to retrieve the DateTime value from the server.

+7
entity-framework
source share
3 answers

Is there a reason why you simply cannot drop it into your database? If you include DateTime.Now in an entity query, it will push it (getdate) to the database.

Linq example for objects

var dQuery = dbContext.CreateQuery<DateTime>("CurrentDateTime() "); DateTime dbDate = dQuery.AsEnumerable().First(); 

SQL Generated ..

 SELECT GetDate() AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] 

Maybe the best way to do this?

+13
source share

This is the update @ Nix response for EF4:

 var dateQuery = dbContext.Database.SqlQuery<DateTime>("SELECT getdate()"); DateTime serverDate = dateQuery.AsEnumerable().First(); 
+5
source share

In VS 2008, if you add a function template to return a scalar, it will not add code to simplify its use. You need to directly access the function template - I use a partial class to create the necessary methods for usability. They recorded this in VS2010.

  public DateTime GetDateTime() { var returnValue = new DateTime(); using (var connection = new EntityConnection(Connection.ConnectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = "myStoredProc"; command.CommandType = CommandType.StoredProcedure; try { returnValue = Convert.ToDateTime(command.ExecuteScalar()); } finally { connection.Close(); } } } return returnValue; } 

Additional Information: Importing Functions in an Entity Model with a Non Entity Return Type

0
source share

All Articles