Question

I use entity framework and Im trying to make such a query generic, so it can be used for any entity type (with an assumption that each entity has property int Id).

I tried something like this, but there is no collection ctx.TEntity or something similiar:

public class Queries<TEntity> where TEntity : AbstractEntity
{
  public Func<AdventureWorksEntities, int, TEntity> getQuery() {
   return
    CompiledQuery.Compile<AdventureWorksEntities, int, Entity>(
    (ctx, num) => ctx.TEntity.First(x => x.Id>num));
    }
}

AbstractEnitity:

public abstract class AbstractEntity {

[Key]
public int Id {get; set};
}

Thanks for your ideas :)

Was it helpful?

Solution

I tried Compiled Queries with a DbContext, with no success (it is not supported, and the workaround were not suitable for me too). Have you checed this link : http://blogs.msdn.com/b/efdesign/archive/2011/06/30/auto-compiled-linq-queries-entity-framework-june-2011-ctp.aspx ?

You can see this article : http://social.msdn.microsoft.com/Forums/en-US/0c07e1d6-7db6-4348-b106-e576d3153b70/ef-40-compiled-queries-performance?forum=adonetefx. I don't think using Compiled Queires make a big difference espacially with modern versions of EF (5 & 6).

On the first query, the connection is set up, and it can take some time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top