禁用CAST AS以优化实体框架中的查询 [英] Disable CAST AS to optimize query in Entity Framework

查看:69
本文介绍了禁用CAST AS以优化实体框架中的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Entity Framework 5 and I want to select data from Oracle 10g database. Problem is that the database table is huge and the query generated by Entity Framework is ineffective. I want to get rid of those CAST( [column] AS [type] ). Is there any setting to turn them off?

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

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

What I want is the code to generate better performing query:

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

推荐答案

迟到总比没有好)

如果使用代码优先和手动映射类,则将HasColumnType("INT")配置用于int属性.

If you are using code first and manual mapping classes, use HasColumnType("INT") configuration for int properties.

例如:

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

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

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

这篇关于禁用CAST AS以优化实体框架中的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆