Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable CAST AS to optimize query in Entity Framework

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# code:

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

The generated query:

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
like image 910
Taai Avatar asked Oct 20 '22 05:10

Taai


1 Answers

Better later than never )

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

For example:

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

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

entity
    .Property(x => x.ID)
    .HasColumnType("INT");
like image 149
ringess Avatar answered Oct 22 '22 17:10

ringess