泄漏实体框架的抽象 - 只是一点点

时间:2014-10-27 20:42:27

标签: c# linq entity-framework-6 oracle12c

如果我想访问RDBMS特有的功能怎么办?有没有办法将数据库特定的SQL注入到EF生成的SQL中?

例如,在Oracle 12c中,您可以为DDL和DML添加时间有效性(有很多种类;我在这里只使用简单的示例):

这可以在C#中很好地建模:

[TemporalAxis("valid_time")]
public class SomeEntity
{
  public string SomeField { get; set; }
}

然后与LINQ一起使用

var results = context.SomeEntities
                .Where(i => i.SomeField = "some_value")
                .AsOfPeriodFor("valid_time", dateVariable);

.AsOfPeriodFor扩展程序可以是这样的:

public static class TemporalExtensions
{
    public static IEnumerable<TSource> AsOfPeriodFor<TSource>(this IEnumerable<TSource> source, 
        string temporalAxis, DateTime asOfDate)
    {
        // reflect on TSource to ensure it has the correct attribute/value (else degrade/throw)
        // do something to source that sets up EF to add the extra clause in the DML

        return source;
    }
}

我猜DbContext可以反映其实体在初始化时驱动DDL(我将不得不详细了解这一点)。

以上结果是EF将发出以下SQL

DDL(初始化时):

create table some_table
  some_field varchar2(30)

  period for valid_time -- Need to inject this at DB Initialize time
);

DML(在查询时):

select
  some_field
from 
  some_table
  as of period for valid_time to_timestamp('27-Oct-14') -- and this at query time
where
  some_field = 'some_value';

我的问题:是否有可用于将这些RDBMS专业短语注入EF生成的SQL的钩子或IInterfaces?或者必须将上述内容与自定义Oracle数据库提供程序结合使用?这种类型的事情是否可行,您能指出我可以提供指导的博客/书籍/视频/专家吗?

2 个答案:

答案 0 :(得分:1)

据我所知,我无法修改由EF提供​​程序生成的SQL。

但是,对于这些特殊情况,您可以直接运行SQL。

context.Database.SqlQuery<SomeEntity>("select * from SomeEntity " +
"some more custom sql here " +
"where somecomlumn = @p1", parameter1);

您必须确保返回的内容符合SomeEntity的形状。

答案 1 :(得分:0)

实施拦截器:EF-Tutorial

看起来像这样:

class EFCommandInterceptor: IDbCommandInterceptor
        {
            public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
            {
                LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
            }

            public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
            {
                LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync,  command.CommandText));
            }

            public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContextt<System.Data.Common.DbDataReader> interceptionContext)
            {
                LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
            }

            public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
            {
                LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
            }

            public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
            {
                LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
            }

            public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
            {
                LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
            }

            private void LogInfo(string command, string commandText)
            {
                Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
            }
        }

但是如何获取类型来获取我现在还不知道的元数据。