Very similar to CreateQuery would be following:
var id = 42;
using(var ctx = new Entities()){
var query = ctx.Companies.Where(o=>o.Id==id);
var result = query.First();
}
Before I call First() query is just a query. Nothing has been sent to the database. Only when data is asked will the query get executed and data retrieved.
Writing lambda is easy, but lets say you can sacrifice that for other benefits. If EDMX is not aware of your data mapping you basically can only use ExecuteStoreQuery. Ex. you have manually created mapping.
var queryString = "SELECT ... FROM " + tableName;
var table = context.ExecuteStoreQuery<ResultTableTemplate>(queryString );
Writing code for your application is fine, but sometimes database outlive several User Interfaces. In order to minimize amount of work needed to be done you can store some if not all functionality in the database. They can be stored there mainly as functions or procedures.
ExecuteFunction is only for function imports. Functions are computed values and cannot perform permanent environmental changes to SQL Server (i.e. no INSERT or UPDATE statements allowed). To call a custom function Select Today()
in C# :
var date = ctx.ExecuteFunction<DateTime>("Today").First();
Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. By default it is lazy loading, that is why you see me using .First()/.FirstOrDefault/.ToList/... for more information on how to load data as you need you can take a look at Loading Related Entities.
Procedures are like database batch scripts. If you use database first design then most likely most of your business logic will be stored in procedures. You would call them in c# like :
var cmdText = "[DoStuff] @Name = @name_param, @Age = @age_param";
var @params = new[]{
new SqlParameter("name_param", "Josh"),
new SqlParameter("age_param", 45)
};
ObjectContext.ExecuteStoreQuery<MyObject>(cmdText, @params);
Bonus:
One common thing you would want to do is to call a stored procedure that accepts a table value parameter.