Entity Framework Compiled Query

后端 未结 2 1168
醉酒成梦
醉酒成梦 2020-12-06 08:35

How do I write this Entity Framework LINQ Query as a Compiled Query?

var context = new SlxDbContext();
var userSet = context.Set();
User user = u         


        
2条回答
  •  渐次进展
    2020-12-06 08:42

    Unfortunately the version of EF you are using (code first), does not support compiled queries.

    Please correct me if I'm wrong.

    Some links:

    How do I precompile an Entity Framework Code-First Query?

    EF Code First DbContext and Compiled Queries

    http://blogs.msdn.com/b/adonet/archive/2011/03/02/ef-4-1-is-coming-dbcontext-api-amp-code-first-rtw.aspx

    UPDATE:

    Here is a sample for compiled queries, but I think it's not going to work with Code First:

    public static Shop CompiledGetShopById(Guid shopId)
    {
        using (DataContext dtx = new DataContext(ConfigProvider.ConnectionString)) {
            return Compiled_GetById.Invoke(dtx, shopId);
        }
    
    }
    
    private static Func Compiled_GetById = 
        Objects.CompiledQuery.Compile(
            (DataContext db, Guid shopId) => 
                (from item in db.Shops where item.ShopId == shopId)
                .FirstOrDefault()
        );
    

提交回复
热议问题