gpt4 book ai didi

c# - 禁用 CAST AS 以优化 Entity Framework 中的查询

转载 作者:太空狗 更新时间:2023-10-30 01:35:42 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 5,我想从 Oracle 10g 数据库中选择数据。问题是数据库表很大,Entity Framework 生成的查询无效。我想摆脱那些 CAST( [column] AS [type] )。有什么设置可以关闭它们吗?

C#代码:

var context = new APPDB();
var q = context.APP_TABLE.Where(i => i.ID == 123);

// This is how I did get the generated SQL query
var str = ((System.Data.Objects.ObjectQuery) q ).ToTraceString();

生成的查询:

SELECT 
CAST( "Extent1"."ID" AS number(10,0)) AS "C1",
"Extent1"."DESCRIPTION" AS "DESCRIPTION"
FROM "APP"."APP_TABLE" "Extent1"
WHERE (123 = ( CAST( "Extent1"."ID" AS number(10,0))))

我想要的是生成性能更好的查询的代码:

SELECT 
"Extent1"."ID" AS "C1",
"Extent1"."DESCRIPTION" AS "DESCRIPTION"
FROM "APP"."APP_TABLE" "Extent1"
WHERE
"Extent1"."ID" = 123

最佳答案

迟做总比不做好)

如果您使用代码优先和手动映射类,请为 int 属性使用 HasColumnType("INT") 配置。

例如:

var entity = builder.Entity<APP_TABLE>();

entity
.HasKey(x => x.ID)
.ToTable("APP_TABLE", "SCHEMA");

entity
.Property(x => x.ID)
.HasColumnType("INT");

关于c# - 禁用 CAST AS 以优化 Entity Framework 中的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24801627/

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