I have a simple question: if I have a simple EF query and I execute it, than it works like follows:
it generates an sql query
it runs the query against the database
it fills the entity object list with data from a simple datareader?
Thanks.
Related
We are using DTOs server side, and have configured a dbcontext using fluent api in order to give breeze the metadata it needs. We map 1:1 to our real database entities, and each DTO contains a simple subset of the real database entity properties.
This works great, but now I need to figure out a way to make queries efficient - i.e. if the Breeze client queries for a single item I don't want to have to create a whole set of DTO objects before I can filter. i.e. I want to figure out a way to execute the filter/sort on the actual entities, but still return DTO objects.
I guess I need to figure out a way to intercept the query execution in order to query my real database entities and return a DTO instead of the real database entity.
Any ideas for how to best approach this?
Turns out that if you use projection in a link statement, e.g.
From PossibleCustomer As Customer
In Customers
Select New CustomerDto With {.Id = PossibleCustomer.Id,
.Name = PossibleCustomer.Name,
.Email = PossibleCustomer.Email}
.. then linq is smart enough to still optimize any queries to the database - i.e. if I query on the linq statement above to filter for a single item by Id, the database is hit with that query for just a single item and a single DTO is created. Pretty clever stuff. This only works if you do a direct projection in the linq statement - if you call off to a function to create your DTO then this won't work.
Just in case others are facing the same scenario, you might want to look at AutoMapper - it can create these projections for you using a model you create just once - avoids all those huge linq statements that are hard to read and validate. The automapper projections (assuming you stick to the simple stuff) still allow the linq to entities magic that ensures you don't have to do table scans when you create your DTOs.
I'd like to create an entity from a custom SQL query. Is that possible with Entity Framework 4?
I remember now. ExecuteStoryQuery will do just that.
See Directly Executing Store Commands
I am trying to learn and start using entity framework 5.0
I was able to create my data model (Only one table), but I am not able to find the commands / functions / methods that allow me to send sql queries and access the results and save and update...etc. Can someone write a very small block of code to show how to do so?
You can write raw SQL like so to return entities:
var entities = context.MyEntities.SqlQuery("SELECT * FROM dbo.MyEntities").ToList();
Or for non entities:
var myAttributeValues = context.Database.SqlQuery<string>(
"SELECT MyAttribute FROM dbo.MyEntities").ToList();
However, you should not use raw sql just because you prefer it. You should use it in situations where raw sql provides you with an advantage over generated queries. If you prefer to write SQL then you should reconsider whether you really want to use EF or any other ORM at all. See here for some EF documentation and examples and here for info on querying for entities.
I am looking for a way to let my users run linq queries against a database.
For example, the user might type "from p in product select p.Name" and then get the results in the UI.
Ideally, it would use the Entity Framework to do its job.
Is this possible?
Unless you want to code your own parser and build expression trees or compile queries at runtime then probably no. But you can check Entity SQL - it is like SQL run on Entity model.
Or you can install LinqPad to your users - but that is really corner solution.
If I write a LINQ to Entities query does that get translated to a native query that the provider understands (i.e. SqlClient)?
OR
Does it get translated to Entity SQL that the Entity Framework then translates to a native query and passes to the provider?
If it gets translated to Entity SQL where I would I be able to see the Entity SQL that was generated?
If my question reveals that I'm totally screwed up in my thinking please set me straight!
LINQ to Entities does not get translated to Entity SQL. Both LINQ to Entities and Entity SQL go to what is called a "Canonical Command Tree" which the provider then converts to store-specific SQL. I've seen a couple of people who should know better claim otherwise, but MS documentation indicates the above is correct.
(source: microsoft.com)