gpt4 book ai didi

c# - Entity Framework 使编译查询通用

转载 作者:行者123 更新时间:2023-11-30 15:32:13 24 4
gpt4 key购买 nike

我使用 Entity Framework 并且我试图使这样的查询通用,因此它可以用于任何实体类型(假设每个实体都具有属性 int Id)。

我试过这样的东西,但是没有集合 ctx.TEntity 或类似的东西:

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));
}
}

抽象实体:

public abstract class AbstractEntity {

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

谢谢你的想法:)

最佳答案

我尝试使用 DbContext 编译查询,但没有成功(它不受支持,而且解决方法也不适合我)。你检查过这个链接了吗:http://blogs.msdn.com/b/efdesign/archive/2011/06/30/auto-compiled-linq-queries-entity-framework-june-2011-ctp.aspx

可以看到这篇文章:http://social.msdn.microsoft.com/Forums/en-US/0c07e1d6-7db6-4348-b106-e576d3153b70/ef-40-compiled-queries-performance?forum=adonetefx .我认为使用 Compiled Queires 不会对现代版本的 EF(5 和 6)产生重大影响。

在第一次查询时,连接已建立,这可能需要一些时间。

关于c# - Entity Framework 使编译查询通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19490186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com