How to execute a raw sql query from LINQ to Entities 4.5

.net-4.5entity-framework-5linq-to-entitiessql-server-2008-r2

Problem

I need to execute a raw SQL query from LINQ to Entities and retrieve the result. The query returns the current date/time from the SQL Server instance, and looks like this:

SELECT GETDATE()

[Edit]
I'm using a data model that was created database-first.
[/Edit]

What I've Tried

I've researched this issue on the interwebz and been unable to find a technique to do this. I was able to learn how to do this using LINQ to SQL, but since I'm not using that, it's of no help.

Best Answer

Heres what you are after

var time = context.Database.SqlQuery<DateTime>("SELECT GETDATE()").FirstOrDefault();

You can read more about raw sql and EF here http://msdn.microsoft.com/en-us/data/jj592907.aspx

Related Topic