views:

30

answers:

2

In LINQ, you can write a manual SQL query, take the results from it and have LINQ "map" those into the appropriate properties of your Model classes (or at least, I'm pretty sure I read you can do that).

Is it possible to do something like that in Entity Framework?

I have an web app that's using EF, and it's CPU usage is ridiculously high compared to the traffic it has, and profiling it in my machine, all that time is (predictably) spent in the DB functions, and the largest portion of that time (> 85%) is spent by EF "generating" SQL and doing stuff before actually executing a query.

So my reasoning is that I can just go in and hardcode the SQL queries, but still use my populated Model properties in my view.

Is this possible? If so, how would I do it?

Thanks!
Daniel

A: 

You can see the generated code by EF designer and do the same thing. Even though you can download tt template that could generate customized codes for mapping sql and stored procedures results.

Jahangir Zinedine
+3  A: 

So what you want to do is hydrate an object from an IDataReader? It's pretty easy to write code to do this (hint: reflection! or you can get fancy and use a member initialization expression) or you can Google for myriad existing implementations on the Internet.

You can do this within EF using ObjectContext.ExecuteStoreQuery<T> or ObjectContext.Translate<T> if you already have a DbDataReader.

Jason
But esentially LINQ has a feature where this is done automatically, you don't need to use reflection, you just feed it the results (I can't remember if it's an IDataReader specifically, but something close to that), and it'll give you back the IList<YourModel>. The question is whether EF has such a feature.
Daniel Magliola
Oh, then I think what you want is `ObjectContext.ExecuteStoreQuery<T>`, no? Sorry, I initially thought you were subverting EF. (Also, you meant LINQ-to-SQL, not LINQ.)
Jason
It sounds like that is exactly what I need. Is that an EF4 only thing? I'm using the oldest version of EF in the planet (although i'm considering upgrading)
Daniel Magliola
Yes, that's new to EF4. Upgrade now.
Jason