EF6 Oracle TimeStamp & Date

datetime entity-framework-6 oracle

Question

I'm starting to use EntityFramework 6 with Oracle (client 12.x). I have severals tables with DATE columns and TIMESTAMP columns.

All queries generated by EF6 convert .NET DateTime type to TIMESTAMP. So the query performance is very poor.

I tried to add a precision to 0 for the DATETIME columns but nothing changes on the generated query.

Property(_ => _.MyDate).HasColumnName("DATE_COLUMN").HasPrecision(0);

How can I specify in my code to translate my where clause by a TO_DATE function and not by a TO_TIMESTAMP function ?

1
0
2/10/2016 8:39:29 AM

Popular Answer

I've had a similar issue with DATE Oracle type and EF6.1. My workaround was to use the CodeFirstFunctions library (available only for EF6.1 and later) and specify a conversion function first in Oracle:

create or replace FUNCTION TO_DATE_LOCAL 
(
  DATETIMETOCONVERT IN VARCHAR2 
, CONVERTFORMAT IN VARCHAR2 
) RETURN DATE AS 
BEGIN
  RETURN to_date(DATETIMETOCONVERT, CONVERTFORMAT);
END TO_DATE_LOCAL;

And later in my DbContext:

[DbFunction("CodeFirstDatabaseSchema", "TO_DATE_LOCAL")]
public static DateTime ToDateLocal(string dateTimeToConvert, string convertFormat)
{
    // no need to provide an implementation
    throw new NotSupportedException();
}

So I can force Entity Framework to use DATE type in a where condition:

var measurement = 
  context.Measurements
    .Where(m => m.MeasuredAt == 
          PlantContext.ToDateLocal("2016.01.01 10:00:00", "YYYY.MM.DD Hh24:MI:SS"))
    .FirstOrDefault();

Be careful to use capital letters for the function name and for the schema name with CodeFirstFunctions if you use Oracle.

If you need more details I've written a blog post about this with an example project.

1
9/29/2016 4:56:21 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow