EF takes the expression and parses that into corresponding SQL. It’s not that the linq query directly is sql itself.Entity framework also supports adding views from our database and apoun adding and saving the same, code is automatically generated which allows us to fetch data from our view.Now consider the following example:
var result = from b in ctx.View_Inventory_Total select b;
Now here I am assuming that entity framework has generated a class corresponding to my view named as View_Inventory_Total. In order to make things clear just put a breakpoint after you just fetch the data from the result object using foreach loop.
And then switch over to the entity framework profiler.
Now you would be able to view what query is hitting against Database for that view. This would not had been possible in normal cases where programmer never is aware of what query a view is executing.
Now we come on to the stored procedures. Consider we have a stored procedure named CustomerSales and we try fetching data directly from it in our code, but we will find out that SP does not exist because entity framework needs typed data in order to convert them to classes. So right click on the stored procedure through server explorer and click “Add function import” and select your stored procedure from the dialogue box. And now you have to specify the return type of the SP. Assuming that you don’t have any information on it you can select ‘Complex Type’ and Just create an entity for yourself by clicking on Ok.
Now if you go to your code and try rewriting the query you would be able to find out a new method which has been generated by entity framework that corresponds to your SP.
Returned by this query will be a complex type of which attributes would be known at compile time.
In order to dive in deep and view how code functions it is not a good idea to put a breakpoint and try viewing it through that. .net installs some samples with it in your installation directory as shown below:
It is inside C:\Program Files (x86)\Microsoft Visual Studio 9.0\Samples\1033
Now we have to click on csharp samples and then on linq samples and then expressiontreevisualizer directory. Now extract it to desktop and open the solution file.
Build the solution and navigate to Bin>Debug directory inside expressiontreevisualizer and look for a dll named ExpressionTreeVisualizer.dll.
Now navigate to Visualizers folder inside my documents->Visual studio 2008 as:
And move the dll to the visualizers folder. Now stop and restart your visual studio 2008
Now write a linq query and fetch the expression as shown below:
Now click on the visualization sign from the code and you will see the tree as follows:
In the image above we can see several expressions and an expression tree. This whole information is passed on to .net compiler which in turn converts this into a SQL query and fetches data from database.
1).LINQ-to-SQL v/s LINQ-to-entities
LINQ-To-SQL is not essentially an ORM and it can’t do the mapping part. Linq to sql basically generates classes for all the tables in your Database. I will explain more of this in a while.
2).Linq-to-entities v/s N-Hibernate
N-Hibernate is a very powerful ORM tool for asp.net. Some of those differences are:
EF Has a designer, N-Hibernate doesn’t
Actually N-Hibernate also has many designers available like Active writer, Visual NHibernate, LLBLGen etc. But normally developers generate code through command prompt utilities and then handcraft it for efficiency.
EF is from Microsoft
As EF is a Microsoft product so it has got plenty of support and documentation available over internet. But it is very common to see that majority of .net Data access methods don’t survive over 2 years.
EF comes inbuilt with visual studio 2010 so it has got much better integration with .net but N-Hibernate as well integrates very well with all the .net components like WCF,RIA etc.
As EF is tied up with Microsoft so it is bound to have future releases and improvements well within time.