EF6 Oracle TimeStamp & Date

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 ?


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.

链接地址: http://www.djcxy.com/p/36946.html

上一篇: scn如何使用sys.smon映射到时间戳

下一篇: EF6 Oracle时间戳和日期