如何在EF6中执行oracle用户定义函数

本文关键字:oracle 用户 定义 函数 执行 EF6 | 更新日期: 2023-09-27 18:19:54

我在edmx:中生成了一个用户定义的函数

<Schema Namespace="DealerMaintenance.Store" Provider="Oracle.ManagedDataAccess.Client" ProviderManifestToken="11.2" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">

  <Function Name="GET_NEXT_DEALER_KY" Aggregate="false" BuiltIn="false"
     NiladicFunction="false" IsComposable="true"
     ParameterTypeSemantics="AllowImplicitConversion" Schema="MY_SCHEMA"
     ReturnType="number" />

我试过了:

public partial class DealerContext : DbContext
{
    [DbFunction("DealerMaintenance.Store", "GET_NEXT_DEALER_KY")]
    public int? GetNextDealerKy()
    {
        var lObjectContext = ((IObjectContextAdapter)this).ObjectContext;
        var output = lObjectContext.
                CreateQuery<int?>("DealerMaintenance.Store.GET_NEXT_DEALER_KY")
            .Execute(MergeOption.NoTracking)
            .FirstOrDefault();
        return output;
    }
}

调用时使用:

using (var objectContext = new DealerContext())
{
  var x = objectContext.GetNextDealerKy();
}

得到:

"System.Data.Entity.Core.EntitySqlException"类型的异常在EntityFramework.dll中发生,但未在用户代码中处理

附加信息:"DealerMaintenance.Store.GET_NEXT_DEALER_KY"无法在当前范围或上下文中解析。确保所有引用的变量都在作用域中,所需的模式是已加载,并且命名空间被正确引用。近成员访问表达式,第1行,第24列。

我还尝试了以下变体:

        using (var objectContext = new DealerContext())
        {
            var queryText = "SELECT DealerMaintenance.Store.GET_NEXT_DEALER_KY() FROM dual";
            ObjectQuery<int> adQuery = ((IObjectContextAdapter)objectContext).ObjectContext.CreateQuery<int>(queryText);
            var x =  adQuery.First();
        }

得到:

无法在当前作用域或上下文中解析"dual"。确保所有引用的变量都在作用域中,加载了所需的架构,并且正确引用了命名空间。

如何在EF6中执行oracle用户定义函数

我无法让函数直接工作。解决方法是使用oracle命令:

        using (DealerContext objectContext = new DealerContext())
        {
            string connString = objectContext.Database.Connection.ConnectionString;
            using (var conn = new OracleConnection(connString))
            {
                using (var cmd = new OracleCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "select MY_SCHEMA.GET_NEXT_DEALER_KY FROM DUAL";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection.Open();
                    try
                    {
                        var kyo = (decimal)cmd.ExecuteScalar();
                        return Decimal.ToInt32(kyo);
                    }
                    finally
                    {
                        cmd.Connection.Close();
                    }
                }            
            }
       }

的上下文类使用:

    public int GetNextDealerKy()
    {
        string connString = this.Database.Connection.ConnectionString;
        using (var conn = new OracleConnection(connString))
        {
            using (var cmd = new OracleCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = "select MY_SCHEMA.GET_NEXT_DEALER_KY FROM DUAL";
                cmd.CommandType = CommandType.Text;
                cmd.Connection.Open();
                try
                {
                    var kyo = (decimal)cmd.ExecuteScalar();
                    return Decimal.ToInt32(kyo);
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
        }
    }