Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Entity SQL vs Query builder methods [closed]

Query a Conceptual Model.

I know in EF exists 3 option for querying:

  • Linq to Entity
  • Entity SQL
  • Query builder methods

Here an example of code for the last two:

#region Query Entity Sql
string queryJob4 = @"SELECT VALUE myJobs FROM CmsJobs AS myJobs WHERE myJobs.JobId = @id";
ObjectQuery<CmsJob> queryJobs4 = new ObjectQuery<CmsJob>(queryJob4, context);
queryJobs4.Parameters.Add(new ObjectParameter("id", 58));
#endregion

#region Query Builder method
ObjectQuery<CmsJob>queryJob5 = context.CmsJobs.Where("it.JobId == @id", new ObjectParameter("id",66));
#endregion

I would like to know:

  • In which environment it is more appropriate one way to another and why?
  • What do you use and why?

Thanks guys for your opinions!

like image 510
GibboK Avatar asked Feb 04 '11 10:02

GibboK


2 Answers

Linq to Entity is the easier way to build most queries and it's strongly typed, so if you could use Linq To Entity, I would use this as the first choice. However, there are certain situations that you cannot use Linq to Entity, then you have to use Entity SQL or Query builder methods.

One point is that if you want to use Entity Framework outside of VB or C# programs, you may not be able to use LINQ to Entities.

like image 195
J.W. Avatar answered Oct 05 '22 09:10

J.W.


Most of my code uses Linq because thats what EF is essentially about. I'm switching to my own SQL only if I suspect the EF to build queries which are not performant enough. My opinion is that if you write more QueryBuilder-stuff than using Linq, EF might be the wrong technology for that particular project. HTH.

like image 29
Alexander Schmidt Avatar answered Oct 05 '22 09:10

Alexander Schmidt