I don't want for my current ModifeidDateTime to be machine time already server time. Every time I want to update or add a person to my datebase on SQL Server 2008 I want to fill ModifiedDateTime filed. It's not like I can change update query as with data adapter command when I work with dataset and to define for my ModifiedDateTime filed to be =GetDate(). I created stored function to retutn me a value of GetDate() method, but I have a problem to import procedure which returns values as int, string ... or no value at all, already just entity values as Person for example in my case. Why is that? Anyway, It would be of great help if you can provide me any solution that works to retrieve DateTime value from server.
+2
A:
Is there a reason you just can't push it down to your database? If you include DateTime.Now in your entity query, it will push it down (getdate) to the database.
Example linq to entities
var dQuery =dbContext.CreateQuery<DateTime>("CurrentDateTime() ");
DateTime dbDate = oquery.AsEnumerable().First();
SQL Generated ..
SELECT GetDate() AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
Might be a better way to do it ?
Nix
2010-04-06 14:11:16
This works just fine. I found out about this datetime canonical functions, but I didn't know how to use them from code behind. I saw that they can be used in CSDL. Thanks.
Levelbit
2010-04-06 14:57:12
A:
In VS 2008, if you add a function template to return a scalar, it does not add the code to make it easy to use. You need to access the function template directly -- I use the partial class to build the needed methods for ease of use. They fixed 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;
}
More information: http://stackoverflow.com/questions/578536/function-imports-in-entity-model-with-a-non-entity-return-type
bryanjonker
2010-04-06 14:37:32